Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 · 2 comments

Comments

@archer87pl
Copy link

archer87pl commented Apr 24, 2024

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):

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
@ajcvickers
Copy link
Member

@archer87pl The repro code in your example project throws the same exception on 6.0.29 as it does on 8.0.4.

@archer87pl
Copy link
Author

@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?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants