asp.net-mvc – 无法更改关系,因为一个或多个外键属性在MVC 4中不可为空

点击保存(更新)我的表单后,我收到此错误

The relationship Could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship,the related foreign-key property is set to a null value. If the foreign-key does not support null values,a new relationship must be defined,the foreign-key property must be assigned another non-null value,or the unrelated object must be deleted.

这是我的控制器(案例“保存”在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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....