I built a very simple MVC3 application to do a little demo, but I’m running against a problem; I return an entity to my view, edit it and then post it back, but in this process my entity loses it’s change-tracking capabilities. When I return the entity to my view it’s still an entity framework proxy class, but when it comes back from my view it’s a ‘Person’ class (the entity is called person).
Here’s my repository class:
public class PersonRepository : IPersonRepository
{
public EfContext Uow { get; set; }
public PersonRepository(IUnitOfWork uow)
{
Uow = uow as EfContext;
}
// yada yada yada
public void Add(Person person)
{
Uow.Persons.Add(person);
}
}
This entity is sent to my view that has a simple form, created with Html.EditorForModel. After that I post it back to this method:
[HttpPost]
public ActionResult Edit(Person person)
{
if (ModelState.IsValid)
{
_personRepository.Add(person);
_personRepository.Uow.Commit();
return RedirectToAction("Index");
}
return View(person);
}
And tada, it’s no longer a tracked proxy class. This results in a primary key violation because entity framework is trying to add my object as a new object, where I just want entity framework to detect the changes and create an update statement instead. Oh by the way, the Commit method in the code above just calls SaveChanges(), here’s the class:
public class EfContext : DbContext, IUnitOfWork
{
public DbSet<Account> Accounts { get; set; }
public DbSet<Person> Persons { get; set; }
public void Commit()
{
SaveChanges();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
By the way, this is my entity class:
public class Person
{
[HiddenInput(DisplayValue = false)]
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Account> Accounts { get; set; }
}
Does anyone know how to fix this? I had this working before as far as I can remember, I just don’t know how.
Thanks in advance!
Define an
Updatemethod in your repository that will attach the entity and mark it as modified.