Entity framework primary key update


















James Kolpack 9, 2 2 gold badges 41 41 silver badges 57 57 bronze badges. R01 R01 2 2 gold badges 4 4 silver badges 14 14 bronze badges. I'm no expert with entity framework but can't you set the object's key to be eventid since it's an auto-increment column anyway? Duplicate of: stackoverflow. Add a comment. Active Oldest Votes. Just don't update primary keys. Instead delete one row and insert a new one. Alternatively you can update the primary key directly with a stored procedure or other query.

Sign up or log in Sign up using Google. Active 10 months ago. Viewed k times. Attach book ; db. Entry book. Modified; db. Grigory Zhadko 1, 1 1 gold badge 15 15 silver badges 27 27 bronze badges. But Still did not get it why this code is failing? Am not expert in this topic, I can't answer this question. You haven't changed anything. Playing with Entity state won't change the fact that the object hasn't actually been modified.

Well, I did the same as you and didn't get the error. How did you handle the concurrency? Did you use a timestamp, did you clone and then merge the objects again or did you use self-tracking entities?

If you didn't handle the concurrency, I guess that's the problem. Show 4 more comments. Active Oldest Votes. Craig W. Assigning the value doesn't update the database, calling db. SaveChanges with modified objects in the context updates the database.

Still it fascinates me Because the context generated the object the context can track the object, including changes to the object. When you call SaveChanges the context evaluates all the objects it is tracking to determine if they are added, changed, or deleted and issues the appropriate SQL to the connected database.

Modified not working. Only thing working is - you need to retrieve the object, make desired changes, and save it via db. SaveChanges ; — Gurpreet Singh. You should NOT have to retrieve the object first in order to update it. I had the same problem until I realized I was trying to change one of the primary key values composite key. As long as you provide a correct primary key, you can set the EntityState to Modified and SaveChanges will work, provided you don't break some other integrity constraint defined on the table.

Show 7 more comments. Find item. Entry entity. Hope this help! Miguel Miguel 3, 2 2 gold badges 15 15 silver badges 30 30 bronze badges. No need to enumerate all properties. I assume SaveChanges call is required after setting values.

Yes, changes will be persisted on SaveChanges — Miguel. Update newObj then SaveChanges ; Your solution updates the whole object without having to loop through all the properties.

Make sure the IDs match between the two models and it will update just fine. Show 10 more comments. You can use the AddOrUpdate method: db. Migrations; db. SaveChanges ;. El Mac 2, 7 7 gold badges 30 30 silver badges 50 50 bronze badges. IMO best solution — Norgul. AddOrUpdate is used during database migration, it is highly discouraged to use this method outside of migrations, hence why it's in the Entity. Migrations namespace. Popular Answer As per my knowledge primary key once generated cant be updated programatically,that defies the purpose of primary key.

View more on Stack Overflow. Prime Library. Not affiliated with Stack Overflow. SaveChanges vs BulkSaveChanges. Try for free now. You can also configure multiple properties to be the key of an entity - this is known as a composite key. Composite keys can only be configured using the Fluent API; conventions will never set up a composite key, and you can not use Data Annotations to configure one. For more information, see the documentation on value generation.

You can configure the name of the primary key constraint as follows:. While EF Core supports using properties of any primitive type as the primary key, including string , Guid , byte[] and others, not all databases support all types as keys. In some cases the key values can be converted to a supported type automatically, otherwise the conversion should be specified manually. Key properties must always have a non-default value when adding a new entity to the context, but some types will be generated by the database.

In that case EF will try to generate a temporary value when the entity is added for tracking purposes. After SaveChanges is called the temporary value will be replaced by the value generated by the database.



0コメント

  • 1000 / 1000