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

查看:157
本文介绍了EF 6.1.1-无法更改关系,因为一个或多个外键属性不可为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试删除实体之间的关系并创建新实体时遇到此错误。

I am getting this error when trying to remove relations between entities and creating new ones.

public partial class Course
{
    public int Course_ID { get; set; }
    /* CODE*/
    public virtual ICollection<Student> Students { get; set; }
}

public partial class Student
{
    public int Student_ID { get; set; }
    /* CODE*/
    public virtual ICollection<Course> Courses { get; set; }
    public virtual ICollection<Book> Books { get; set; }
}

public partial class Book
{
    public int Book_ID { get; set; }
    public int Student_ID { get; set; }
    /* CODE*/
    public virtual Student Student { get; set; }
}

关系如下:


  • 课程*-*学生(学生和课程之间具有多对多关系,因此,在标准化过程中创建了Student_Courses表-一个学生可以参加多门课程,并且一门课程多个学生)

  • 学生1-*书(学生与书具有一对多关系-一个学生有很多书,一本书属于一个学生)

我正在尝试像这样更新学生实体:

I'm trying to update a Student Entity like this:

    public bool UpdateEntity(Entities.Student student)
    {
        using (var context = new Entities())
        {
            var record = context.Students
                                .Include("Courses")
                                .Include("Books")
                                .FirstOrDefault(c => c.Student_ID == student.Student_ID );

            // record.Courses.ToList().ForEach(s => record.Courses.Remove(s));
            // record.Books.ToList().ForEach(t => record.Books.Remove(t));

            record.Courses.Clear();
            record.Books.Clear();

            record.Courses= student.Courses;
            record.Books= student.Books;
            context.SaveChanges();

            return true;
        }
    }

应删除学生与课程之间的关系而不删除实际课程。
更新书与学生之间的关系时,也应删除该书。

A relation between a student and a course should be removed without removing the actual course. When updating a relation between a book and a student, the book should be deleted as well.

编辑
更新以使其更清晰

EDIT updated to make it clearer

推荐答案

当您要从数据库中删除多个集合中的项目时,必须标记每个删除项目,例如像这样:

When you want to remove items in a 1-many collection from the database you have to mark each item for delete, e.g. like so:

foreach (var book in record.Books)
{
    context.Books.Remove(book);
}
context.SaveChanges();

有时候这很麻烦。当您 Clear 一个集合导航属性时,EF谨慎行事,并尝试仅中断关联而不删除项目,即它试图使外键无效。但是,EF 知道 FK字段在数据库中不能为空,因此在我看来EF 应该能够弄清楚这种情况是用户的打算删除项目,而不是孤立它们。

It's a nuisance sometimes. When you Clear a collection navigation property, EF errs on the side of caution and tries to only break the association without deleting the items, i.e. its tries to nullify the foreign keys. However, EF does know that the FK field is not nullable in the database, so it seems to me that EF should be able to figure out that in this case it's the user's intention to delete items, rather than to orphan them. But too bad, it doesn't.

我曾经与NHibernate和IIRC一起工作,NHibernate确实删除了类似情况下的子项,甚至触发了一次删除操作( 从Child删除,其中Child.ParentId = ... )。 EF分别为每个孩子触发一个删除语句。

I used to work with NHibernate and IIRC, NHibernate did delete child items in similar cases, even firing a one-shot delete (delete from Child where Child.ParentId = ...). EF fires a delete statement for each child separately.

这篇关于EF 6.1.1-无法更改关系,因为一个或多个外键属性不可为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆