违反了多重约束。角色' Student_LendedBooks ...'关系' LibarySystem.DataModel'具有多重性1或0..1

时间:2017-02-05 13:21:01

标签: c# .net entity-framework

当我尝试使用Entity Framework向我的学生添加书籍时,它会抛出异常"违反了多重性约束。角色' Student_LendedBooks_Source'关系' LibarySystem.DataModel.Student_LendedBooks'具有多重性1或0..1"。我该如何解决?我对实体框架不是很熟悉。谢谢你的帮助。

DbContext类:

public class DbContext : System.Data.Entity.DbContext {

    public DbSet<Student> Students { get; set; }
    public DbSet<Book> Books { get; set; }

}

学生班:

public class Student {

    public Student() {
        LendedBooks = new HashSet<Book>();
    }

    [Key]
    public string PESEL { get; set; }

    public string Name { get; set; }
    public string SecondName { get; set; }
    public string Surname { get; set; }
    public string Class { get; set; }
    public virtual ICollection<Book> LendedBooks { get; set; }

}

图书课程:

public class Book {

    public Book() {
        IsLend = false;
    }

    [Key]
    public string CatalogueNumber { get; set; }

    public string StudentPesel { get; set; }
    public virtual Student Student { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public DateTime? DateOfLend { get; set; }
    public DateTime? DateOfReturn { get; set; }
    public bool IsLend { get; set; }

}

添加方法:

public static void AddBookToStudent(Student student, Book book) {
        using (var context = new DbContext()) {
            var findStudent = context.Students.Find(student.PESEL);
            var findBook = context.Books.Find(book.CatalogueNumber);
            if (findBook != null) {
                findBook.DateOfLend = DateTime.Today;
                findBook.DateOfReturn = book.DateOfLend + new TimeSpan(7, 0, 0, 0);
                findBook.StudentPesel = findStudent?.PESEL;
                findBook.Student = findStudent;
                findBook.IsLend = true;
            }
            findStudent?.LendedBooks.Add(book);
            context.SaveChanges();
        }
    }

3 个答案:

答案 0 :(得分:0)

此代码存在多个设计问题。但对于初学者来说,'Lended Book'必须是一个独立的实体,它将(FK)连接到学生和书本。它将是一个“关联表”。

public class Student {

    public Student() {
        LendedBooks = new List<LendedBook>();
    }

    [Key]
    public string PESEL { get; set; }

    public string Name { get; set; }
    public string SecondName { get; set; }
    public string Surname { get; set; }
    public string Class { get; set; }

    public virtual List<LendedBook> LendedBooks { get; set; }
}

public class Book {

    public Book() {
    }

    [Key]
    public string CatalogueNumber { get; set; }

    public string Name { get; set; }
    public string Author { get; set; }
}

public class LendedBook
{
    public DateTime DateOfLend { get; set; }
    public DateTime? DateOfReturn { get; set; }

    // TODO: Annotations, ForeignKey
    public string StudentPesel { get; set; }
    public virtual Student Student { get; set; }

    // TODO: Annotations, ForeignKey
    public string CatalogueNumber { get; set; }
    public virtual Book Book { get; set; }
}

public static void AddBookToStudent(Student student, Book book) {
    using (var context = new DbContext()) {
        var findStudent = context.Students.Find(student.PESEL);
        var findBook = context.Books.Find(book.CatalogueNumber);

        if (findBook != null) {
            // Something like ...
            findStudent?.LendedBooks.Add(new LendedBook() {
                DateOfLend = DateTime.Today,
                DateOfReturn = book.DateOfLend + new TimeSpan(7, 0, 0, 0),
                Book = findBook
            });

            context.SaveChanges();
        }
    }
}

答案 1 :(得分:0)

如果您要将Book添加到一个Student

然后我相信你有一个错字:

findStudent?.LendedBooks.Add(book);

应该成为:

findStudent?.LendedBooks.Add(findBook );

答案 2 :(得分:0)

您正确设置了findBook.Student属性,但随后将方法参数book添加到findStudent.LendedBooks。如果该图书中还有其他Student设置,那么您将会遇到不好的时间。

findStudent?.LendedBooks.Add( findBook )块内移动findBook != null来电,按照书面说明,将book替换为findBook