-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Owned Entity marked as detached when adding to a collection when the entities primary key is set #18055
Comments
@AndriySvyryd - The state printed is actually detached. The issue is ran into due to https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#dc var query = db.Blogs.ToList();
var blog = db.Blogs.Single();
var newPost = new Post
{
BlogId = blog.BlogId,
PostId = Guid.NewGuid(), // Comment this line out and everything works
Name = "Post 1"
};
db.Add(newPost);
blog.Post.Add(newPost);
// Prints "Detached"
Console.WriteLine(db.Entry(blog.Post.Single()).State);
// Throws DbUpdateConcurrencyException
db.SaveChanges(); |
I don't think this is the expected behavior. |
This is an unexpected behavior. In some cases we would like to let the database generate our identifier, and in others we need to accept an identifier to synchronize entities across services. The mitigation would be to not let the database generate the identifier and ensure we always create our own? |
We're also encountering this exact issue, not just with owned entities, but also with one-to-many relationships. I see this issue has been tagged with 3.1.0 release. If there any plan to release a patch before then? We rely heavily on this behavior. Is there a recommended workaround? |
@samisq We don't plan to patch this. 3.1 is scheduled for release on December 3, and any patch would be unlikely to ship much before this anyway. |
Note for triage: this seems to be working as designed with current bits. Specifically:
The solution is to mark the property to not use generated key values: post.Property(e => e.PostId).ValueGeneratedNever(); Bringing back to triage before closing to see if I missed anything. |
I think 0259104 fixed it |
Just as an FYI, I have been working on migrating an app from EF Core 2.2 to 3.0 and the number one runtime issue we have been running into is what I describe in this issue. We have a lot of places where we manually set PKs as Guid's in C# code instead of generating it on the SQL server, which behaves completely different in 3.0. I have been able to find most of the issues thanks to our unit tests, but we only have about 70% coverage so I am not confident that I have found them all. I do understand that it is specifically called out in the upgrade docs, but to me this seems to contradict the "why" section of those docs. It seems like it less consistent now than it was before. For example, some types (string, datetime, etc) behave differently now than other types (int, guid, etc) when they are part of the entity's key. Having to remember which types will and wont be generated by the backing store and remembering to add |
@ChristopherHaws I think it is fair to say that it is now more important than before to understand whether a key is configured to use generated values or not. However, Guid and int keys have always been configured by default to use generated values--this hasn't changed. It also isn't new that entity types with generated key values behave differently. The only thing that has changed in 3.0 is that this different behavior now also applies when DetectChanges is called. So previously when I added a specific note to dotnet/EntityFramework.Docs#1046 (comment) to ensure that this is well documented. |
Thanks for the clarifications @ajcvickers I have created an interface
Finally, we have a
|
I have a simple entity which owns many children. If I add a child entity with the PK set, EF marks the added child entity as detached instead of added.
In EF Core 2.2.x this behavior worked, however it no longer seems to work in EF Core 3.0.
Steps to reproduce
When using InMemory database, this code produces the following exception:
When using SqlServer, it produces the following exception:
Further technical details
EF Core version: 3.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET Core 3.0
Operating system: Windows 10 1809
IDE: Visual Studio 2019 16.3, Visual Studio 2019 16.4 Preview 1, and LINQPad 6.
The text was updated successfully, but these errors were encountered: