-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Adding an Entity Graph with sub-n-level entities with the same Id results in error - Breaking Change #19984
Comments
@jprsilva I am not able to reproduce this. Please also post the code from OnModelCreating that configures this model. |
I will do it. |
@ajcvickers can you use my test project? Code OnModelCreating / OnConfiguring:
|
@ajcvickers were you able to reproduce the error? |
@jprsilva Yes. The code is attempting to track two entities with the same key value. I ran this code on 2.2 and got the same behavior. I suspect that your repro code is not setting up the same scenario as is failing in your real application. Without being able to reproduce the issue I can't say for sure, but I have a hunch it is related to this: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#dc |
@ajcvickers Thanks for your feedback. Code with EFCore 2.2.6
Yes, I believe this is related to that breaking change. |
@jprsilva [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BookId { get; set; } This means that when the value is set EF will treat the entity as an existing entry with the given key value. For example, here: Book = new Book
{
BookId = 200, If you want to specify the key value for new entity instances, then configure EF for that: [DatabaseGenerated(DatabaseGeneratedOption.None)]
public int BookId { get; set; } If you want the database to generated key values, then don't explicitly set the BlogId property. |
@ajcvickers the error is not related to the BookId. Did you run my tests? The error is related when I have two instances of the same type with the same Id in a graph of entities, when I Add that graph to the context in EFCore this results in a error. As you can see in EFCore 2.2.6 (and also EF6) this kind of error doesn’t happen. Do you understand now what I am trying explain you? I guess if you run both codes you can see the problem / difference. |
@jprsilva Do you want key values for |
Yes. |
@jprsilva Then don't set BookId. Leave it set to the CLR default--that is, zero. |
@ajcvickers if the book is an existing one I need to set it’s Id (did you saw the title “Existing Book”?). |
Let me make a recap. Assume I have a disconnected graph of entities that I want to attach to EF Context. In the previous versions the Add method didn’t throw any error when adding a full disconnected graph, because an untracked entity found by DetectChanges would be tracked in the Added state. Now with EFCore 3, with the breaking change: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#dc it throws an error because it is guessing the track state of each entity and it doesn’t allow to have two entities of the same type with the same Id with the state Modified - of course. The reported ideia is good. I am only focusing the Add method: the attach / tracking of the entities. So my desire is to know how to disable this behavior in EFCore 3. I want to be able to add a disconnected graph and all the untracked entities beeing tracked as added - despite their key values and the database generate key flag. After that I run my own helper class to update the track state of each entity in that graph. Hope you can understand and help me. |
I think this is similar to what you said here: #20116 (comment) But like I already said, for me, and for the moment, if I could disable this new behavior in EF Core 3 and return to the old behavior, it would fix my problems. Thanks |
@jprsilva I went back and re-investigated from the beginning. It turns out that in 2.2 your code was making use of this bug: #18007. So EF was, incorrectly, silently discarding the duplicate instances of the Author entity. Unfortunately, this behavior, though a bug, is what you were using to get the behavior you wanted. EF Core does not, in general, allow multiple instances with the same key to be tracked, regardless of the state of the entity. (Deleted entities can have special behavior, but that's not relevant here.) The approach of going through an invalid graph state in order to then fix it up later is not recommended, even though the limitations of EF6 made it a common approach there. (Note that EF6 still required identity resolution to a single instance.) To fix this:
|
Makes sense. Thanks very much for your time and explanation. |
EFCore 3.1.1 - breaking change
Adding a disconnected entity graph to EF Context, if there is an entity of the same type with the same Id this results in an error.
In the previous version 2.2 and in the EF6 this was working fine.
Steps to reproduce
Adding the shelf entity to the EFCore context results in the error:
Message:
The instance of entity type 'Author' cannot be tracked because another instance with the key value '{AuthorId: 2}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
StackTrace:
Further technical details
EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 4.6.1
Operating system: Win 10 Pro 1909
IDE: Visual Studio 2019 16.4.5
The text was updated successfully, but these errors were encountered: