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 – 应用程序池循环如何影响ASP Net会话状态?
- asp.net-mvc – mvc razor @helper可以返回非编码标签吗?
- asp.net jquery+ajax异步刷新实现示例
- asp.net 根据汉字的拼音首字母搜索数据库(附 LINQ 调用方法
- asp.net-mvc – ASP.NET MVC身份默认实现
- asp.net-mvc – 无法在Web服务器上启动调试. Web服务器找不
- .net – Viewstate隐藏字段如此之大,一切都会崩溃
- ASP.NET MVC与Webforms:替换WebForms控件
- 如何通过Asp.net WebAPI中的异常过滤器传递内容?
- ASP.Net核心 – 获取帖子表格的所有数据
- asp.net-mvc – 使用ASP.NET MVC进行项目组织的最
- asp.net 禁用viewstate在web.config里
- 如何使用ASP.NET MVC Web API OData链接到Razor中
- asp.net – 我如何使用AJAX来确定用户的会话是否
- ASP.NET MVC删除操作链接确认
- 部署DotNetNuke并将ASP.NET应用程序分开 – 可能
- asp.net-mvc – 如何在asp.net mvc中处理分页?
- asp.net-mvc – 在asp.net mvc中启动一组未选中的
- asp.net实现文件下载的代码
- ASP.NET Webforms,用户控件中的JavaScript
