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
We are using generic entity classes for JSON deserialization and Entity Framework Core 6.
Here is an example of two related entities:
public class Vendor
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Category> Categories { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
[ForeignKey(nameof(ParentId))]
public Category Parent { get; set; }
}
When deserializing JSON data from a provider, there may be a situation where there will be two instances of the same object type with the same identifiers (Id).
As a result, data manipulation may result in an error:
System.InvalidOperationException: "The instance of entity type 'Category' cannot be tracked because another instance with the key value '{id: 1}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached."
At the same time, attempts to disable tracking do not lead to anything: db.ChangeTracker.AutoDetectChangesEnabled = false;
It is possible to solve this problem by creating a separate dictionary for the data type, and using the key to "reassign" the correct instances, but this is very difficult for tree-like structures.
Is this some kind of flaw or is there a solution to this problem?
The text was updated successfully, but these errors were encountered:
We are using generic entity classes for JSON deserialization and Entity Framework Core 6.
Here is an example of two related entities:
When deserializing JSON data from a provider, there may be a situation where there will be two instances of the same object type with the same identifiers (Id).
As a result, data manipulation may result in an error:
At the same time, attempts to disable tracking do not lead to anything:
db.ChangeTracker.AutoDetectChangesEnabled = false;
It is possible to solve this problem by creating a separate dictionary for the data type, and using the key to "reassign" the correct instances, but this is very difficult for tree-like structures.
Is this some kind of flaw or is there a solution to this problem?
The text was updated successfully, but these errors were encountered: