abp icon indicating copy to clipboard operation
abp copied to clipboard

abp ef Required one-to-many with shadow foreign key insert error

Open 842549829 opened this issue 1 year ago • 5 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Description

abp ef Required one-to-many with shadow foreign key insert error. If the column below abp is not used, it can be inserted normally.

Error message: The instance of entity type 'Course' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.

Example:

builder.Entity<Course>(b =>
 {
       b.HasOne(x => x.Teacher).WithMany(t => t.Courses);
 });
public class Teacher : FullAuditedAggregateRoot<Guid>
 {
       public string Name { get; set; }

       public string Title { get; set; }

       public ICollection<Course> Courses { get; set; } = new List<Course>();
 }
public class Course : FullAuditedAggregateRoot<Guid>
    {
        public string Name { get; set; }

        public Teacher? Teacher { get; set; }
  }

Test:

 public class TeacherAppService : CrudAppService<
          Teacher, 
          TeacherDto, 
          Guid, 
          PagedAndSortedResultRequestDto, 
          CreateUpdateTeacherDto>
  {
      public TeacherAppService(IRepository<Teacher, Guid> repository) : base(repository)
      {
          Repository=repository;
      }

      public IRepository<Teacher, Guid> Repository { get; }

      public override async Task<TeacherDto> CreateAsync(CreateUpdateTeacherDto input)
      {
          var teacher = new Teacher
          {
              Name = "My Name",
              Title = "My Title"
          };
          var course1 = new Course { Name = "Course1" };
          var course2 = new Course { Name = "Course2" };
          teacher.Courses.Add(course1);
          teacher.Courses.Add(course2);
          await Repository.InsertAsync(teacher);
          return null;
      }
}

Reproduction Steps

No response

Expected behavior

No response

Actual behavior

No response

Regression?

No response

Known Workarounds

No response

Version

8.0

User Interface

Common (Default)

Database Provider

EF Core (Default)

Tiered or separate authentication server

Tiered

Operation System

Windows (Default)

Other information

No response

842549829 avatar Jun 25 '24 01:06 842549829

try:

public class Teacher : FullAuditedAggregateRoot<Guid> { public string Name { get; set; }

   public string Title { get; set; }

   [ForeignKey("TeacherId")]
   public ICollection<Course> Courses { get; set; } 

}

mrh520 avatar Jun 25 '24 05:06 mrh520

The Microsoft website documentation describes this configuration method as Required one-to-many with shadow foreign key https://learn.microsoft.com/en-us/ef/core/modeling/relationships/one-to-many#required-one-to-many-with-shadow-foreign-key

842549829 avatar Jun 28 '24 03:06 842549829

public class TeacherAppService : CrudAppService<
     Teacher,
     TeacherDto,
     Guid,
     PagedAndSortedResultRequestDto,
     CreateUpdateTeacherDto>
{
    public TeacherAppService(IRepository<Teacher, Guid> repository) : base(repository)
    {
        Repository=repository;
    }

    public IRepository<Teacher, Guid> Repository { get; }

    public override async Task<TeacherDto> CreateAsync(CreateUpdateTeacherDto input)
    {
        var teacher = new Teacher
        {
            Name = "My Name",
            Title = "My Title"
        };
        var course1 = new Course { Name = "Course1",Id=Guid.NewGuid() };
        var course2 = new Course { Name = "Course2", Id=Guid.NewGuid() };
        teacher.Courses.Add(course1);
        teacher.Courses.Add(course2);
        await Repository.InsertAsync(teacher);
        return null;
    }
}

mrh520 avatar Jun 28 '24 08:06 mrh520

I don't understand what you mean

842549829 avatar Jun 28 '24 09:06 842549829

Assign a value to the Id

mrh520 avatar Jun 28 '24 10:06 mrh520

I know assigning an Id will work but what I want is for the framework to generate the Id for me by default

842549829 avatar Jul 01 '24 01:07 842549829

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 26 '25 06:04 stale[bot]