Skip to content
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

Feature Request: Updating the database with different entity reference #7261

Closed
groege opened this issue Dec 16, 2016 · 2 comments
Closed

Feature Request: Updating the database with different entity reference #7261

groege opened this issue Dec 16, 2016 · 2 comments

Comments

@groege
Copy link

groege commented Dec 16, 2016

It's about the problem solved with this stackoverflow post:
http://stackoverflow.com/questions/7968598/entity-4-1-updating-an-existing-parent-entity-with-new-child-entities/7969372#7969372

Let's say we have Entity "A" with the children Entity "A1".
I want load "A" & "A1" in a context which i close right after.
After a while i have replaced the list with another "A1" (new object, same primary key), now when opening a new context trying to update "A" we run into an exception:

Exception message:
System.InvalidOperationException: 'The instance of entity type 'TransportProject' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type). If you are explicitly setting key values for new entities, ensure they do not collide with existing entities or temporary values generated for other new entities. When attaching existing entities, ensure that only one entity instance with a given key value is attached to the context.'

Wouldn't it be helpful not to have to do all the work ourselves (as described in the link at the top) but rather trust the framework does this automatically?
Any reasons why it has been decided to implement it this way? Are there any plans or is there a chance this improvement will be implemented?

Thanks!

Further technical details

EF Core version: 1.1.0
Database Provider: SqLite
Operating system: Windows 10 Mobile
IDE: Visual Studio 2017 RC

@ajcvickers
Copy link
Member

@groege We have attempted to do such things automatically in the past and found it fraught with problems, especially when it comes to graphs of entities where the relationships are different in different graphs. Also, when people are not expecting this behavior it can become very difficult to figure out what is happening--for example, I asked the context to track this object, but actually it just updated a different object and the first object is still not being tracked. All that being said, having a higher-level solution to this kind of issue is on our backlog--see #5536.

For now, if there are two object instances that represent the same entity, then it is the application's responsibility to know when this is happening and apply changes from one object to the other as appropriate. For example, if the context is already tracking one object, then changes made to another object with the same identity can be applied using something like this:

context.Entry(post).CurrentValues.SetValues(newPost);

This will take the property values from newPost and set these values on the tracked post .

@groege
Copy link
Author

groege commented Dec 20, 2016

Thanks for the info :) ! Will keep a close look at #5536

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants