asp.net-mvc – 实体框架4不保存我的多对多行
|
希望是一个非常简单的问题.但是我正在使用一个MVC应用程序的Code First,并且我有一个具有多对多关系的Category和ServiceType对象: public class Category
{
public Category()
{
ServiceTypes = new HashSet<ServiceType>();
}
public Guid CategoryId { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
public virtual ICollection<ServiceType> ServiceTypes { get; set; }
}
数据库已正确生成,并包含名为CategoryServiceTypes的链接表.我的问题是我将项目添加到我的ServiceTypes集合并调用save,虽然没有发生错误,但是没有行添加到CategoryServiceTypes.当以下代码到达SaveChanges时,category.ServiceTypes的计数为1,所以在集合中肯定有一些东西: [HttpPost]
public ActionResult Edit(Category category,Guid[] serviceTypeIds)
{
if (ModelState.IsValid)
{
// Clear existing ServiceTypes
category.ServiceTypes.Clear();
// Add checked ServiceTypes
foreach (Guid serviceType in serviceTypeIds)
{
ServiceType st = db.ServiceTypes.Find(serviceType);
category.ServiceTypes.Add(st);
}
db.Entry(category).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
我希望我在这里做明显的错误.有任何想法吗? 谢谢. 编辑: 虽然以下回应确实是正确的答案,我以为我会添加以下最后版本的编辑帖子方法: [HttpPost]
public ActionResult Edit(Category category,Guid[] serviceTypeIds)
{
if (ModelState.IsValid)
{
// Must set to modified or adding child records does not set to modified
db.Entry(category).State = EntityState.Modified;
// Force loading of ServiceTypes collection due to lazy loading
db.Entry(category).Collection(st => st.ServiceTypes).Load();
// Clear existing ServiceTypes
category.ServiceTypes.Clear();
// Set checked ServiceTypes
if (serviceTypeIds != null)
{
foreach (Guid serviceType in serviceTypeIds)
{
ServiceType st = db.ServiceTypes.Find(serviceType);
category.ServiceTypes.Add(st);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
注意强制加载ServiceTypes集合的行,这是需要的,因为延迟加载不包括那些子项,这意味着清除ServiceTypes集合什么都不做. 解决方法尝试将您将类别附加到行前面的循环前面的上下文:[HttpPost]
public ActionResult Edit(Category category,Guid[] serviceTypeIds)
{
if (ModelState.IsValid)
{
// Clear existing ServiceTypes
category.ServiceTypes.Clear();
db.Entry(category).State = EntityState.Modified;
// category attached now,state Modified
// Add checked ServiceTypes
foreach (Guid serviceType in serviceTypeIds)
{
ServiceType st = db.ServiceTypes.Find(serviceType);
// st attached now,state Unchanged
category.ServiceTypes.Add(st);
// EF will detect this as a change of category and create SQL
// to add rows to the link table when you call SaveChanges
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
在您的代码中,EF不会注意到您已经添加了服务类型,因为当servicetypes已经在category.ServiceTypes集合中并且所有服务类型已经被附加到上下文中时,将类别附加到上下文. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 在ASP.NET中拒绝用户时,’CustomIdentity’上的Serializati
- owin – 如何在Startup.cs中添加CamelCasePropertyNamesCon
- ASP.NET MVC Web应用程序中的控制器是否应该调用存储库,服务
- [转]谈谈技术原则,技术学习方法,代码阅读及其它
- asp.net-mvc – asp.net mvc:int值的千位分隔符
- Asp.Net Cache,从缓存修改对象,并更改缓存值
- asp.net – jQuery的ajax正在导致FireFox的全页刷新
- ASP.Net中的Vb6“Tag”属性等价物?
- asp.net-mvc – 绑定到Viewmodel的文件上传
- 在Textbox asp.net上使用JQuery DatePicker
- asp.net-mvc-3 – Asp.Net MVC 3 – @ Html.Acti
- 结帐后asp.net – “无效的标记’,’在类,结构或
- asp.net-mvc – 根据浏览器接受语言自动设置uiCu
- asp.net-mvc – ASP.net MVC数据注释DateTime默认
- ASP.NET Core WebApi将错误消息返回给AngularJS
- asp.net-mvc – ASP.Net MVC:发送JSON到控制器
- asp.net-mvc – 如何在asp.net mvc中使用SOAP
- 如何处理ASP.NET vNext中的调试/发布配置转换
- ASP.NET和C#代码中的常量
- 如何在ASP.Net Web API中为特定控制器启用Window
