简体   繁体   English

操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系asdf

[英]The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable, asdf

Scenario is: 场景是:

I have Two Tables. 我有两张桌子。

  1. Enrollment 注册
  2. EnrollmentSubject. EnrollmentSubject。

There is One to Many Relationship between them. 它们之间存在一对多的关系。

I'm trying to update Enrollment by removing childs in EnrollmentSubject And Adding New Child, I face error below when I context.SaveChanges(); 我正在尝试通过在EnrollmentSubject中删除子项并添加新子项来更新Enrollment,当我context.SaveChanges();时,我会遇到以下错误。 in Entityframework 6.0 在EntityFramework 6.0中

The operation failed: 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

Here is my code: 这是我的代码:

public string UpdateEnrollment(EnrollmentDTO enrollDetail)
{
        try
        {


            List<EnrollmentSubjectDTO> subjectsDTO = new List<EnrollmentSubjectDTO>();
            using (SMSEntities em = new SMSEntities())
            {
                Enrollment enroll = (from aa in em.Enrollments.Include("EnrollmentSubjects")
                                     where aa.EnrollmentId == enrollDetail.EnrollmentId
                                     select aa).FirstOrDefault<Enrollment>();
                enroll.Status = enrollDetail.Status;
                enroll.RegNo = enrollDetail.RegNo;
                enroll.PkgId = enrollDetail.PkgId;
                enroll.SectionId = enrollDetail.SectionId;
                enroll.AdmittedDate = enrollDetail.AdmittedDate;
                enroll.StudentId = enrollDetail.StudentId;
                subjectsDTO = enrollDetail.EnrollmentSubjects.ToList<EnrollmentSubjectDTO>();

                List<EnrollmentSubject> enrolllist = enroll.EnrollmentSubjects.ToList<EnrollmentSubject>();
                if (enroll.ClassId != enrollDetail.ClassId && enroll.GroupId != enrollDetail.GroupId)
                {
                    foreach (EnrollmentSubject obj in enrolllist)
                    {
                        enroll.EnrollmentSubjects.Remove(obj);
                    }
                }
                enroll.ClassId = enrollDetail.ClassId;
                enroll.GroupId = enrollDetail.GroupId;
                for (int i = 0; i < subjectsDTO.Count; i++)
                {
                    if (subjectsDTO[i].EnrollmentSubjectId == 0)
                    {
                        enroll.EnrollmentSubjects.Add(new EnrollmentSubject()
                    {
                        Status = subjectsDTO[i].Status,
                        SubjectId = subjectsDTO[i].SubjectId
                    });
                    }
                    else
                    {
                        enrolllist[i].SubjectId = subjectsDTO[i].SubjectId;
                        enrolllist[i].Status = subjectsDTO[i].Status;
                    }

                }

               // em.Enrollments.Add(enroll);

                em.SaveChanges();
                return "Enrollment Updated";
            }
        }
        catch(Exception ex)
        {
            return ex.Message.ToString();
        }
}

please guide me 请指导我

My problem is solved...... I was removing it from parent, childs records become Orphans with Parent=null in required relationship in which child cannot exists without parent. 我的问题解决了……我将其从父级中删除,子级记录成为必需关系中的父级为null的孤儿,没有子级就无法存在子级。 but I they were in the context so I also removed them from context. 但是我在上下文中,所以我也将它们从上下文中删除。

 if (enroll.ClassId != enrollDetail.ClassId && enroll.GroupId != enrollDetail.GroupId)
                    {
                        foreach (EnrollmentSubject obj in enrolllist)
                        {

                            enroll.EnrollmentSubjects.Remove(obj);
                            em.EnrollmentSubjects.Remove(obj);
                        }
                    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 操作失败:无法更改关系,因为一个或多个外键属性不可为空 - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系。 - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. EF 6:插入多个-无法更改关系,因为一个或多个外键属性不可为空 - EF 6: inserting multiple - The relationship could not be changed because one or more of the foreign-key properties is non-nullable 由于一个或多个外键属性不可为空,因此无法更改该关系 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable EF添加实体:由于一个或多个外键属性不可为空,因此无法更改关系 - EF adding entity: The relationship could not be changed because one or more of the foreign-key properties is non-nullable 实体框架5无法更改该关系,因为一个或多个外键属性不可为空 - Entity Framework 5 The relationship could not be changed because one or more of the foreign-key properties is non-nullable 由于一个或多个外键属性不可为空,因此无法更改该关系 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable Pocos-无法更改关系,因为一个或多个外键属性不可为空 - Pocos - The relationship could not be changed because one or more of the foreign-key properties is non-nullable EF 6.1.1-无法更改关系,因为一个或多个外键属性不可为空 - EF 6.1.1 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable 实体框架无法更改关系,因为一个或多个外键属性不可为空 - Entity Framework The relationship could not be changed because one or more of the foreign-key properties is non-nullable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM