How to update an entity without a round-trip? (EF 4)

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?

Answers


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();
}

Need Your Help

How to get notification if System Preferences Default Sound changed

osx cocoa notifications core-audio

This is fairly simple (I think). I'm simply wanting to get a notification in my application whenever a user changes the default sound input or sound output device in System Preferences - Sound. I...

Extracting MKAnnotation Callout's Title When Sender

iphone ios mkannotation callouts

Ok guys, so I have a map view with annotations and when tapped, they display callouts with a disclosure icon on the right. When tapped, this function is called: