You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Entity cannot be tracked because another instance with the same key value is already being tracked for optional relationships are not automatically deleted
#33610
Open
archer87pl opened this issue
Apr 24, 2024
· 5 comments
Right now dependenant enties are not being deleted which is causing the error System.InvalidOperationException: The instance of entity type 'CompanyPropertyEntity' 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.
You suggest to use context.SavingChanges event but it is not possible as tracker is in already broken state so the workaround is completly useless.
This is happening when you try to set the new values for simple model (Please note that CompanyId is nullable in CompanyProperty entity):
var company = await GetCompanyById(DefaultCompanyId);
var companyToUpdate = new CompanyEntity()
{
Id = DefaultCompanyId,
Name = "Updated name",
CompanyProperties = new List()
{
new CompanyPropertyEntity()
{
Id = 1,
Name = "Prop 1"
},
new CompanyPropertyEntity()
{
Id = 2,
Name = "Prop 2"
},
}
};
company.CompanyProperties = companyToUpdate.CompanyProperties;
await _db.SaveChangesAsync(); ```
This is riduculus, how should we update the entities in EF 7/8 for such a simple case and avoid throwing that exception?
Is it possible to disable tracking for orpahned entities in EF 7/8?
Example project:
[https://github.com/archer87pl/ef8orphanedentity/tree/main/OrphanedEntities](url)
EF Core version: 8.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.9.6
The text was updated successfully, but these errors were encountered:
@ajcvickers I have updated the repro code.
In earlier revision, the DeleteBehavior for the Entity was set to NoAction, and this caused the code to fail in versions 6.0.29 and 8.0.4. However, when the DeleteBehavior is set to Cascade, the failure only occurs in version 8.0.4. What's the best way to implement a straightforward update in Entity Framework 8?"
@ajcvickers so which solution do you recommend to migrate to EF8 from EF6 for such a configuration? Should we manually process entities (really!?) as we don't want required relationships?
@archer87pl Yes, if you want to delete orphans on optional relationships, then this needs manual processing. Otherwise the relationship will be severed but the entity will not be deleted automatically, since it can exist without a parent, which is the normal behavior for optional relationships. The only reason required relationships are different is that the child entity must have a parent or else be deleted from the database.
Background:
We are doing upgrade from .NET 6.0 to .NET 8.0 and spent several hours identyfing why our code fails in new EF 8.
It appears that there was breaking change in EF Core 7.0 Orphaned dependents of optional relationships are not automatically deleted https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/breaking-changes?tabs=v7#optional-deletes
Right now dependenant enties are not being deleted which is causing the error
System.InvalidOperationException: The instance of entity type 'CompanyPropertyEntity' 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.
You suggest to use context.SavingChanges event but it is not possible as tracker is in already broken state so the workaround is completly useless.
This is happening when you try to set the new values for simple model (Please note that CompanyId is nullable in CompanyProperty entity):
The text was updated successfully, but these errors were encountered: