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 have an object with a complex type, to simplify it, let's consider a Blog with multiple Post. The Blog also has a userId that indicates who created the Blog.
When downloading a list of Blog we get a list of blogs, the posts associated to each Blog, the userId and the User object.
Note: The User can be the same for multiple Blogs, and the User object is a child of Blog.
The strategy is insert or update for now.
The issue we ran into, is that when we have multiple Blog entities, to Add or Update, the User may be the same for multiple blogs and hence the error:
The instance of entity type 'User' 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.
Is it possible to tell Entity that the User object can be duplicated in other entities part of the same transaction?
@antoinecfmws The problem is that resolving two instances of a single entity type into a single instance can get ambiguous and complicated fast. The application should resolve to a single instance before attaching the entities to the context.
We have an object with a complex type, to simplify it, let's consider a
Blog
with multiplePost
. TheBlog
also has a userId that indicates who created theBlog
.When downloading a list of
Blog
we get a list of blogs, the posts associated to eachBlog
, the userId and theUser
object.Note: The
User
can be the same for multiple Blogs, and theUser
object is a child of Blog.The strategy is insert or update for now.
The issue we ran into, is that when we have multiple
Blog
entities, toAdd
orUpdate
, the User may be the same for multiple blogs and hence the error:Is it possible to tell Entity that the
User
object can be duplicated in other entities part of the same transaction?Further technical details
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: 3.1
Operating system: MacOS
IDE: VSCode
The text was updated successfully, but these errors were encountered: