dbContext.<Entity>.Update() is creating new records
See original GitHub issueWhen i do something like:
var e = new Entity();
e.Id = 100;
e.Name = "Name 100";
dbContext.Entities.Update(e);
dbContext.SaveChanges();
a new record is created.
Is this the correct behaviour?
Issue Analytics
- State:
- Created 8 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Entity Framework: updated record being inserted (instead ...
The procedure should be: using (DbContext context = new DbContext() { foreach (Item item in list) { item.Updated = DateTime.Now; context.
Read more >Trouble learning how to Update a record in Entity Framework.
So just making changes to the entity and doing dbcontext.savechanges() would persist changes made to the entity so long as it was retrieved...
Read more >Modifying data via the DbContext
The approach that you adopt to update/modify entities depends on whether the context is currently tracking the entity being modified or not.
Read more >DbContext.Update Method (Microsoft.EntityFrameworkCore)
Update (Object). Begins tracking the given entity and entries reachable from the given entity using the Modified state by default, but see below...
Read more >Update Records in Entity Framework Core
The DbContext's Update() update method is used for performing the updation of records in the database. This tutorial is a part of Entity...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@DiegoZoracKy just to be clear, it only inserts a row if the key is set to zero. If it is set to anything else then it will correctly update it.
i.e. this code
Produces this SQL
Not saying that it’s not a bug… but just that you’ll only see the insert behavior if you are attempting to update an entity with a key set to zero.
Re-opening as we still need this to track the issue where the entity being passed to Update is marked as added if it has no key value assigned.