I tried the following:
public void UpdatePlayer(int id)
{
Player player = new Player() {ID = id};
player.Password = "12";
Entities.Players.Attach(player);
Entities.SaveChanges();
}
No change at the db.
What am I missing?
Entity Framework Core introduced the DbContext. Update() method which attaches the specified entity to a context and sets its EntityState to Modified. Alternatively, you can also use the DbSet. Update() method ( context.
In order to refresh the data entities, you click Data management workspace > Framework parameters > Entity settings > Refresh entity list in D365FO.
This can be achieved in several ways: setting the EntityState for the entity explicitly; using the DbContext. Update method (which is new in EF Core); using the DbContext. Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.
I think it might be because you're setting the values before you attach the object - the data context will not know what fields have changed. Try:
public void UpdatePlayer(int id)
{
Player player = new Player() {ID = id};
Entities.Players.Attach(player);
player.Password = "12";
Entities.SaveChanges();
}
attach is used for entities that already exist in the database, but you have to attach first, and then edit it, as another poster pointed out. you should use .Add instead of .Attach if you are creating new items.
FYI Entity Framework 4 - AddObject vs Attach
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With