讓我解釋一下整個背景:EF4 +波科=問題更新多對多關係
我使用ASP.NET MVC 2,EF4(POCO)。
我想爲我的應用程序做一個通用存儲庫。
我在更新多對多關係有問題。
我有一個與其他許多表有關的項目。在視圖中,用戶選擇所需的類別,並將所選的ID發送到控制器。
然後,控制器詢問類別庫,將其添加到主項:
item.Categories.Add(CategoriesRepository.Single(id);
但是,當我去了倉庫,並試圖挽救這樣的:
Entities.ApplyCurrentValues(entity);
Context.SaveChanges();
但是,我的實體的狀態是增加了。
然後,我無法保存我的實體:(。
我該如何解決這個問題呢?
謝謝您的回答。
我在視圖,下面的代碼:
<%= Html.CheckBoxList("Categories", ((IEnumerable<Categories>)ViewData["Categories"]).ToDictionary(c => c.ID.ToString(), c => c.Name)
, Model.Categories.ToDictionary(c => c.ID.ToString(),c => c.Name)) %>
Where CheckBoxList is a HTMLHelper。
我把視圖中的ID作爲值,因爲我不知道其他方式放置,然後從視圖中獲取此信息。
如何使用ObjectStateManager.ChangeRelationshipState
方法?
是否這樣? :
itemRepository.Db.ObjectStateManager.ChangeRelationshipState(item, item.Categories, "Categories", System.Data.EntityState.Modified);
我以這種方式嘗試,但它返回錯誤。
幫助!大聲笑
可能重複[更新關係節約EF4 POCO的對象發生變化時(http://stackoverflow.com/questions/ 3635071/update-relationships-when-saving-changes-of-ef4-poco-objects) – 2011-02-01 06:18:35