Cannot update AggregateRoot with related entities
GitHub Issues
- Your ABP Framework version -> 4.2.2
- Your database provider -> EF Core
- Exception message and stack trace if available (check the logs)
- The association between entity types 'A' and 'B' has been severed, but the relationship is either marked as required or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, configure the relationship to use cascade deletes. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.
Documentation
I wan't to know how can I update an AggregateRootDTO that has a list of a related entity.
Currently, I have class A that is AggregateRoot
public class A : FullAuditedAggregateRoot<Guid>
{
public A(){
Bs = new HashSet<B>();
}
public virtual ICollection<B> Bs { get; private set; }
}
and Class B, related to A
public class B : Entity<int>
{
public B() { }
public B(int id): base(id) { }
[Required]
public Guid AId { get; set; }
public virtual A A { get; set; }
[Required]
public string StringProperty { get; set; }
}
With respective Dto's
public class A : ExtensibleEntityDto<Guid>
{
public A()
{
Bs = new HashSet<BDto>();
}
public virtual List<BDto> Bs { get; set; }
}
public class BDto : ExtensibleEntityDto<int>
{
public Guid AId{ get; set; }
public string StringProperty { get; set; }
}
I'm also using the CrudAppService<ADto, A, GUID>
When I insert the entity A with one item of B, it works pretty well.
But when I try to update A(even if with no changes, but with the B item loaded), this error occurs:
The association between entity types 'A' and 'B' has been severed, but the relationship is either marked as required or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, configure the relationship to use cascade deletes. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.
Also, when I try to add a new C item on the AggregateRoot, this error happens:
The instance of entity type 'C' cannot be tracked because another instance with the same key value for {'AId', 'Cd'} 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.
Hi, I don't see any problem with the ABP Framework for this issue. It may related about EF Core.
I have the same problem.And I debug it.
The source code in project Volo.Abp.Domain.EntityFrameworkCore -> InsertAsync

Source code in project Abp.EntityFrameworkCore -> EfCoreRepositoryBaseOfTEntityAndTPrimaryKey

They all use DbContext.Add.
In abp context.Add will set entity Id and related entities id.
but in abp.vnext not set related entitied id. so related entities ids all be Guid.Default.
So it can not insert with same id.
DbContext.add works fine in abp, but not in abp vnext.
I can not understand the difference.
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.