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 – 具有一个无效的SelectedValue,因为它在项目列表
- asp.net – TransactionScope是否需要DTC服务?
- asp.net – 成员资格生成密码仅字母数字密码?
- asp.net-mvc – 我的剃刀视图的自定义基页类型,如何使用城堡
- asp-classic – 与SQL Server Compact Edition 4.0的Ado连接
- asp.net – Linq-to-SQL和DateTime的奇怪
- asp.net-mvc – EF实体与服务模型与查看模型(MVC)
- asp.net-mvc – 如何在asp.net mvc中检查模型验证错误?
- asp.net-mvc-3 – 后退按钮或导航到特定视图(页面)-ASP.NET
- 如何基于.NET Core创建ASP.NET项目,而不是基于完整的框架?
- asp.net文件上传示例分享
- asp.net-mvc – 模型单元测试能否真正独立,如何[
- asp.net-mvc-4 – 在MVC4中捕获404错误
- asp.net-mvc – 在HTML表单上的ASP.NET MVC中的P
- 为什么要使用ASP.NET MVC 2进行REST服务?为什么
- 将数据从asp.net-mvc传递到javascript的最佳做法
- asp.net – 如何让Html.RenderAction在帖子上调用
- asp.net-mvc – @ Html.DisplayNameFor的详细信息
- ASP.NET微信公众号用于给指定OpenId用户发送红包
- asp.net-mvc – ASP.NET MVC自定义授权
