I have a scenario where parent entity already exists and I have to add it's child during the update of the parent entity, for example:
var db = new TestContext();
var book = new Book {
BookId = 1, // Already exists in the db from previous save operation
Author = new Author {
FirstName = "Charles",
LastName = "Dickens"
}
};
db.Attach(book);
I am getting the following error in the attach method:
The instance of entity type 'Book' 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.