ASP.NET MVC 3, Entity Framework 4. Update entity with relation to many
I want to update a "Post" and change relationships with "Categories" that already created before. Post entity has ICollection of Categories. But categories are not changed. It seems, that EF does not track entity relations. By the way I have no problem with creating of new Posts with assigning of Categories.
There are two models:
public class Post
{
public virtual int PostId { get; set; }
...
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
public virtual int CategoryId { get; set; }
...
public virtual ICollection<Post> Posts { get; set; }
}
The Add controller, that works as expected:
public ActionResult Create(Post model)
{
va开发者_StackOverflow中文版r c = Request.Form["CategoryID"].Split(',');
model.Categories = c.Select ... .ToList(); //here I assign relationships with attached objects
_repository.Add(model);
_repository.SaveChanges();
...
}
Repository Add method:
T IRepository.Add<T>(T entity)
{
return Set<T>().Add(entity);
}
The Edit controller does not save changed categories, only post props.
public ActionResult Edit(Post model)
{
var c = Request.Form["CategoryID"].Split(',');
model.Categories = c.Select ... .ToList(); //here I update relationships with attached objects
_repository.Attach(model);
_repository.SaveChanges();
...
}
Repository Edit method:
T IRepository.Attach<T>(T entity)
{
var entry = Entry(entity);
entry.State = System.Data.EntityState.Modified;
return entity;
}
Am I doing something wrong?
Thanks in advance
Solution:
public ActionResult Edit(Post model)
{
model = _repository.Attach(model);
var post = _repository.Posts.Include(p => p.Categories).Single(s => s.PostId == model.PostId);
post.Categories.Clear();
model.Categories = GetCategories();
_repository.SaveChanges();
}
- First Attach the object (EntityState.Modified)
- Query the object with Include or other method for Loading Related Entities
- Clear the existent relations. I don like this, but I cannot find another way
- Assign new relations from the view and SaveChanges.
Entity Framework won't track relationship changes this way. It only tracks the states of objects, so the proper way would be to load the "Post" that you want with all categories and then to modify the loaded collection - this way changes are going to be reflected in all objects' states.
Manipulate category collection i.e. (add, remove, edit) in post class. If you are using same DbContext then changes will be tracked. It should be worked.
add a category
_post.Categories.Add(category1);
delete category
_post.Categories.Remove(category1);
edit category
_post.Categories[0].Name = "TEST Name";
udapte a object
string ImgID = CompCert.CertID.ToString() + "ComID" + CompId + ext;
CompCert.CertImageFile = ImgID;
db.ObjectStateManager.ChangeObjectState(CompCert, EntityState.Modified);
db.SaveChanges();
精彩评论