question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dbContext.<Entity>.Update() is creating new records

See original GitHub issue

When 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:closed
  • Created 8 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
rowanmillercommented, Dec 1, 2015

@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

var e = new TestUpdate();
e.Name = "Name 100";
e.Id = 5;
dbContext.TestUpdates.Update(e);
dbContext.SaveChanges();

Produces this SQL

exec sp_executesql N'SET NOCOUNT OFF;
UPDATE [TestUpdate] SET [Name] = @p1
WHERE [Id] = @p0;
SELECT @@ROWCOUNT;
',N'@p0 int,@p1 nvarchar(4000)',@p0=5,@p1=N'Name 100'

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.

0reactions
rowanmillercommented, Dec 3, 2015

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found