2

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)
{
    var 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

4 Answers 4

2

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();
    }
  1. First Attach the object (EntityState.Modified)
  2. Query the object with Include or other method for Loading Related Entities
  3. Clear the existent relations. I don like this, but I cannot find another way
  4. Assign new relations from the view and SaveChanges.
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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";

Comments

0

udapte a object

 string ImgID = CompCert.CertID.ToString() + "ComID" + CompId + ext;
                        CompCert.CertImageFile = ImgID;
                        db.ObjectStateManager.ChangeObjectState(CompCert, EntityState.Modified);
                        db.SaveChanges();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.