Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 3, Entity Framework 4. Update entity with relation to many
    primarykey
    data
    text
    <p>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.</p> <p>There are two models:</p> <pre><code>public class Post { public virtual int PostId { get; set; } ... public virtual ICollection&lt;Category&gt; Categories { get; set; } } public class Category { public virtual int CategoryId { get; set; } ... public virtual ICollection&lt;Post&gt; Posts { get; set; } } </code></pre> <p>The Add controller, that works as expected:</p> <pre><code>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(); ... } </code></pre> <p>Repository Add method:</p> <pre><code>T IRepository.Add&lt;T&gt;(T entity) { return Set&lt;T&gt;().Add(entity); } </code></pre> <p>The Edit controller does not save changed categories, only post props.</p> <pre><code>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(); ... } </code></pre> <p>Repository Edit method:</p> <pre><code>T IRepository.Attach&lt;T&gt;(T entity) { var entry = Entry(entity); entry.State = System.Data.EntityState.Modified; return entity; } </code></pre> <p>Am I doing something wrong?</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload