abp ef Required one-to-many with shadow foreign key insert error
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
try:
public class Teacher : FullAuditedAggregateRoot<Guid> { public string Name { get; set; }
public string Title { get; set; }
[ForeignKey("TeacherId")]
public ICollection<Course> Courses { get; set; }
}
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
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;
}
}
I don't understand what you mean
Assign a value to the Id
I know assigning an Id will work but what I want is for the framework to generate the Id for me by default
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.