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 – MVC 4导出到CSV – 另存为对话框在Chrome和Fire
 - ASP.NET标签控件 – 不编码HTML
 - WPF的Page介绍及Page Window Frame 之间的链接使用示例,嵌
 - asp.net 将一个图片以二进制值的形式存入Xml文件中的实例代
 - asp.net-mvc – 有没有办法重命名RequestVerificationToken
 - ASP.NET Web API,Web服务发现和客户端创建
 - .net – IIS 6.0和ASPX中的404自定义错误不起作用
 - asp.net-mvc-3 – 在使用Unity容器时为此对象异常定义的无参
 - ASP.Net – AJAX UpdatePanel中的Javascript
 - asp.net-mvc-4 – ASP.NET MVC 4通过ActionLink传递对象变量
 
- asp.net-mvc – 为什么我得到一个“无法更新Enti
 - asp.net-mvc – ASP.NET MVC 3列表到IEnumerable
 - asp.net – Mocking HttpContext不起作用
 - asp.net – web部署工具2.1和web部署3.5有什么区
 - asp.net-mvc – ASP.NET MVC Beta 1:DefaultMod
 - Asp.net 实现Session分布式储存(Redis,Mongodb,M
 - asp.net – 如何在MVC 3中设置图表系列颜色?
 - 我应该在ASP.NET MVC中构建我的下一个Web应用程序
 - 服务器端ASP.Net Ajax异常处理
 - asp.net-mvc – ASP.NET MVC:部分知道它是否是从
 
