I’m new to the Entity Framework. I’m using to work with the database in a WPF application. I’ve created a model as in this blog, and I now want to use it in my application.
I know how to retrieve and modify entities like this:
DatabaseContext ct = new DatabaseContext();
var person = ct.Persons.Find(1);
person.Name = "NewName";
ct.SaveChanges();
However, in an application that waits for user input, this means keeping the datacontext around in the ViewModel, which isn’t very desirable. Instead, I want to do something like this:
class PersonViewModel {
Person psn;
public PersonViewModel(int PersonId) {
DatabaseContext ct = new DatabaseContext();
psn = ct.Persons.Find(1);
}
public Save() {
DatabaseContect ct = new DatabaseContext();
ct.Persons.Update(psn); // (There is no Update() method.)
ct.SaveChanges();
}
}
I don’t know how to do Update().
I have the DatabaseContext defined like this:
public partial class DatabaseContext: DbContext
{
public DbSet<Person> Persons { get; set; }
}
but I don’t seem to have DatabaseContext.ObjectStateManager, which some people use in solutions to similar problems. I don’t know why this is – is it related to using POCO objects?
How should I solve this?
As an alternative to Attach: