I have an MVC project and using Entity Framework Code First and POCO objects for a database. For example:
public class ClassA { public int? Id {get; set;} public string Name { get; set;} public virtual ClassB B {get; set;} } public class ClassB { public int? Id {get;set;} public string Description {get;set;} }
I have an ActionResult that creates or edits a model. The problem is that when I call this ActionResult to update the model, and model.B was changed, the relation is not stored in the database. When an ActionResult is called to create a new object, it works as expected. How to solve this?
public ActionResult Save(ClassA model) { model.B = GetBFromDb(model.B.Id.Value); if(ModelState.IsValid) { if (id.HasValue) { context.Entry(model).State = System.Data.EntityState.Modified; } else { context.ClassAs.Add(model); } context.SaveChanges();
c # asp.net-mvc-3 entity-framework ef-code-first
Marthijn
source share