Seeing the issue described in #3890 in 2.0. Also similar to #3953, but with a different provider in my case. An entity containing an Identity key was passed to Update() without the appropriate key set and EFCore created a new row instead of throwing an error, as I would have expected.
I would expect an invocation of Update() to attempt to process the entity as State.Modified and not arbitrarily decide to execute a State.Added action based on the caller omitting the key.
The documentation (https://docs.microsoft.com/en-us/ef/core/) doesn't appear to specify that the Update() method behaves this way. If this is considered "by design", the documentation should explicitly state that passing an entity to Update() without providing a key will create a new row when the key is an identity.
Offending code appears to be Line 71 in EntityGraphAttacher.cs (https://github.com/aspnet/EntityFrameworkCore/blob/dev/src/EFCore/ChangeTracking/Internal/EntityGraphAttacher.cs).
EF Core Version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating System: Win10 Enterprise
IDE: VS 2017
@ssanderlin This is by-design. It is a very useful behavior to have when attaching graphs. Issue to update the documentation is https://github.com/aspnet/EntityFramework.Docs/issues/505 and PR is https://github.com/aspnet/EntityFramework.Docs/pull/541
@ajcvickers Thanks for the response - after reviewing the documentation, I understand the purpose of this change.
Hi All,
I am having issues with the Entity.Update statement. My entity keys are all specified but it is still defaulting to inserting the row based on the error message below, Is there a fix available?
"Cannot insert duplicate key row in object '
@onyand If you think this is a bug--that is, that there are not or should not be duplicate key values--then please file a new issue with a full description of what you are doing and a runnable solution/project or full code listing that will let us reproduce the problem.
Dear Arthur,
Thanks for your feedback. I managed to get a workaround on this by setting the key column attributes with the "IsModified" property to false as shown below.
context.Entry(rowobject).Property(p => p.
Regards,
D.O.
From: Arthur Vickers notifications@github.com
Sent: Tuesday, January 2, 2018 10:17:11 PM
To: aspnet/EntityFrameworkCore
Cc: onyand; Mention
Subject: Re: [aspnet/EntityFrameworkCore] dbContext.
@onyandhttps://github.com/onyand If you think this is a bug--that is, that there are not or should not be duplicate key values--then please file a new issue with a full description of what you are doing and a runnable solution/project or full code listing that will let us reproduce the problem.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/aspnet/EntityFrameworkCore/issues/10194#issuecomment-354851522, or mute the threadhttps://github.com/notifications/unsubscribe-auth/APOJRo9DqPOCqhzFfrd16v8PMnMKQijyks5tGoC3gaJpZM4QMnCv.
@ajcvickers Could this behavior be separated into a different method instead? AddOrUpdate? In my opinion Update should only do that, update, not insert new records. I would expect Update to throw if PK is not provided beforehand, or at least not try to insert a new record.
On a side note, the documentation only mentions disconnected entities, but for the main entity it says it will be added with Modified state no matter what, but that is not the case.
gak paham aku:(
Most helpful comment
@ajcvickers Could this behavior be separated into a different method instead?
AddOrUpdate? In my opinionUpdateshould only do that, update, not insert new records. I would expectUpdateto throw if PK is not provided beforehand, or at least not try to insert a new record.On a side note, the documentation only mentions disconnected entities, but for the main entity it says it will be added with
Modifiedstate no matter what, but that is not the case.