-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
dbContext.<Entity>.Update() is creating new records #3890
Comments
@DiegoZoracKy it should attempt to update an existing record, not insert a new one. Can you share a full code listing that we can use to reproduce the issue? (i.e. the code for |
@rowanmiller i've created something simple to show the case. See: using Microsoft.AspNet.Mvc;
using System.Linq;
using Microsoft.Data.Entity;
using Newtonsoft.Json.Linq;
public class TestUpdate
{
public int Id { get; set; }
public string Name { get; set; }
}
public class MyContext : DbContext
{
public DbSet<TestUpdate> TestUpdates { get; set; }
}
[Route("api/[controller]")]
public class TestController : Controller
{
private MyContext dbContext;
public TestController(MyContext context)
{
dbContext = context;
}
[HttpGet]
public IActionResult Get()
{
var e = new TestUpdate();
e.Name = "Name 100";
dbContext.TestUpdates.Update(e);
dbContext.SaveChanges();
return new ObjectResult(dbContext.TestUpdates.AsNoTracking().ToList());
}
} Every time you hit api/test (GET) a new record will be saved. |
@rowanmiller i've found what it seems to be another weird behavior on public class AnotherClass
{
public int Id { get; set; }
public virtual List<TestUpdate> TestUpdate { get; set; }
} Thinking about SQLServer, this will create a column The problem is that when i do |
@DiegoZoracKy regarding the first issue you reported... This situation is arising because If having a key value of zero is a legitimate requirement, then you can workaround this until we fix the bug by explicitly setting the state (rather than calling
|
@DiegoZoracKy can you open a new issue for the second case you mentioned (and include code that demonstrates the issue again). |
@rowanmiller No, id 0 is not a requirement of my software. I've created this simple example for you to see that the Update method is creating new records. Good that it is a Bug and not its correct behavior. I will create a new issue for sure, within some hours. This current issue is something easy to be avoided, just bringing a performance penalty by checking if the record exists for real before update it. But this last one can be a huge problem. |
@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. |
@rowanmiller i just tried setting a value to var e = new TestUpdate();
e.Id = 100;
e.Name = "Name 100";
dbContext.TestUpdates.Update(e);
dbContext.SaveChanges(); And a new record was created. But for this test i used inMemory ( |
@rowanmiller And, a new issue about the reference being lost during |
@DiegoZoracKy I was able to reproduce that issue with InMemory. It's different from the original bug on this issue (which is already fixed), so I opened #3953 for the new issue. |
Thanks @rowanmiller. I will track this new issue. |
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. |
Issue #3890 When a graph of entities is given to Attach or Update, the state of those entities is set to Unchanged/Modified unless the entity is using key value generation and the key value is not set, in which case the state is set to Added. But for the actual entity passed to these methods (as opposed to additional entities discovered in the graph) we decided that the state should be the requested state regardless of whether or not the key value is set.
Issue #3890 When a graph of entities is given to Attach or Update, the state of those entities is set to Unchanged/Modified unless the entity is using key value generation and the key value is not set, in which case the state is set to Added. But for the actual entity passed to these methods (as opposed to additional entities discovered in the graph) we decided that the state should be the requested state regardless of whether or not the key value is set.
When i do something like:
a new record is created.
Is this the correct behaviour?
The text was updated successfully, but these errors were encountered: