asp.net-mvc – 无法更改关系,因为一个或多个外键属性在MVC 4中不可为空
点击保存(更新)我的表单后,我收到此错误:
这是我的控制器(案例“保存”在swich couse问题): [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(UserModel userModel,string act = null,int idx = 0) { using (var dbContext = new userDbEntities()) { if (userModel.User == null) { userModel.User = new UsersTable(); } var newUser = userModel.User.userID == 0; userModel.CustomTypes = dbContext.CustomTypes.ToList(); switch (act) { case "addcustom": userModel.User.CustomerTables.Add(new CustomerTable { CustomType = new CustomType(),UsersTable = userModel.User }); break; case "deletecustom": userModel.User.CustomerTables.RemoveAt(idx); break; case "save": foreach (var customer in userModel.User.CustomerTables) { customer.CustomType = dbContext.CustomTypes.Find(customer.CustomType.Id_NewCustomerType); } var dbUser = dbContext.UsersTables.Find(userModel.User.userID); dbUser.TimeZoneId = userModel.User.TimeZoneId; foreach (var custom in userModel.User.CustomerTables) { if (custom.CustomerID == 0) dbUser.CustomerTables.Add(custom); } foreach (var custom in dbUser.CustomerTables.ToList()) { var modelCustom = userModel.User.CustomerTables.FirstOrDefault(o => o.CustomerID == custom.CustomerID); if (modelCustom != null) //update it { custom.CustomType = dbContext.CustomTypes.Find(modelCustom.CustomType.Id_NewCustomerType); } if (userModel.User.CustomerTables.All(o => o.CustomerID != custom.CustomerID)) dbUser.CustomerTables.Remove(custom); } dbContext.SaveChanges(); break; } // end switch statements return View("Edit",userModel); } } 任何想法是什么错误… 解决方法尝试如下所示.foreach (var child in modifiedParent.ChildItems) { context.Childs.Attach(child); context.Entry(child).State = EntityState.Modified; } context.SaveChanges(); 请参阅以下链接. http://social.msdn.microsoft.com/Forums/en-US/1833117c-7a93-4b69-a133-b7fd764db810/the-operation-failed-the-relationship-could-not-be-changed-because-one-or-more-of-the-foreignkey?forum=adodotnetentityframework (编辑:商洛站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET中操作SQL数据库(连接字符串的配置及获取)
- asp.net-web-api – WebAPI 2属性路由启用会话状态
- asp.net – 无法返回JsonResult
- asp.net-mvc – MVC“添加控制器”是“无法检索元数据…配置
- asp.net-mvc-4 – MVC 4 Razor如果拆分div标签
- .net – DNU发布 – 来自MSBuild的no-source
- asp.net-mvc-2 – 在名称中使用连字符处理MVC2变量
- 使用asp.net中的web api通过http进行身份验证
- asp.net-mvc-3 – 在MVC Razor View中使用If语句
- asp.net操作xml增删改示例分享
- asp.net – __doPostBack在DotNetNuke网站上未定
- asp.net-mvc – MVC Razor – 如何向自己提交表单
- asp.net-mvc – 尝试将asp.net web发布到Azure时
- asp.net-mvc-3 – 具有最佳实践的示例N层ASP.NET
- asp.net – Visual Studio – 为什么.ASPX文件比
- asp.net-mvc – SSL握手问题? (当时:网页挂起,
- 将mvc 5应用程序连接到Azure中的ACS?
- asp.net-mvc – 尝试创建类型为’TypeNewsContro
- ASP.NET Core中实现用户登录验证的最低配置示例代
- asp.net-mvc – 从视图到控制器POST信用卡数据是