EF Code First与一对多和多对多关系?导致循环或多个级联路径

时间:2013-11-07 19:28:37

标签: ef-code-first

我有四个模特,分别是家庭,母亲,父亲,学生,我正在努力与父亲和父亲联系。母亲和放大器;有家庭桌和学生桌的学生与母亲和父亲桌相关联。此处Family表包含有关该系列的信息。母亲和父亲表包含他们的个人信息,学生表包含学生信息+ MotherID和FatherID。这意味着Family表是Mother和Father,Student表的主表。 Mother表的MotherID和父表的FatherID被引用到Student表。

我正在使用MVC 4实体框架代码,

这些是我的模型类。

public class Family
{

    public int FamilyID { get; set; }
    public string FamilyName { get; set; }

    public virtual ICollection<Mother> Mothers { get; set; }
    public virtual ICollection<Father> Fathers { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}
public class Mother
{
    public int MotherID { get; set; }
    public int FamilyID { get; set; }
    public string FirstName { get; set; }
    public string SSN { get; set; }

    public virtual Family Families { get; set; }
    public virtual ICollection<Student> Students{ get; set; }
}
public  class Father
{
    public int FatherID { get; set; }
    public int FamilyID { get; set; }      
    public string FirstName { get; set; }
    public string SSN { get; set; }

    public virtual Family Families { get; set; }
    public virtual ICollection<Student> Students { get; set; }

}
public class Student
{
    public int StudentID { get; set; }
    public int FamilyID { get; set; }
    public int MotherID { get; set; }
    public int FatherID { get; set; }
    public string FirstName { get; set; }
    public string SSN { get; set; }

    public virtual Family Families { get; set; }
    public virtual Mother Mothers { get; set; }
    public virtual Father Fathers { get; set; }

}

数据库上下文类:

public class MYContext:DbContext
{

    public DbSet<Family> Families { get; set; }
    public DbSet<Mother> Mothers { get; set; }
    public DbSet<Father> Fathers { get; set; }
    public DbSet<Student> Students { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        //base.OnModelCreating(modelBuilder);            
    }


}

编写完上面的代码后,我在包管理器控制台上运行了以下命令。

PM> Add-Migration InitialContext它为我创建了一个InitialContext.cs类。

PM> Update-Database -Verbose它给我一个错误

Introducing FOREIGN KEY constraint 'FK_dbo.Student_dbo.Mother_MotherID' on table                                     
'Student' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON
UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.

enter image description here

我做错了什么?

为什么我收到此错误???请帮我一个人!

首先使用EF代码实现一对多和多对多关系的正确方法是什么?请指导我这个例子。

2 个答案:

答案 0 :(得分:0)

尝试在键上方添加[key]注释。例如,对于学生来说,它看起来像这样......

public class Student
{
    [key]
    public int StudentID { get; set; }
    public int FamilyID { get; set; }
    public int MotherID { get; set; }
    public int FatherID { get; set; }
    public string FirstName { get; set; }
    public string SSN { get; set; }

    public virtual Family Families { get; set; }
    public virtual Mother Mothers { get; set; }
    public virtual Father Fathers { get; set; }

}

这将详细告诉您如何从codefirst角度管理模型。

http://thedatafarm.com/blog/data-access/code-first-entity-framework-4-1-videos-and-articles-on-msdn/

答案 1 :(得分:0)

我在Entity Framework, Code First modeling and a cyclical reference

找到了相同的问题和解决方案

异常来自SQL Server DB而非来自实体框架,具有相同的手工创建约束的表结构将无效。母亲表(Mother.FamilyID)和父亲表(Father.FamilyID),学生表(Student.MotherID)&amp;的外键属性(Student.FatherID)不可为空,表示所需关系的数据库和数据库中的相应列也不可为空。所以DB不允许这种模式。

如果我从模型类中删除所有这些属性,那么自动关系变为可选,因为导航属性可以为null,因此DB被视为另一个模型,因为DB中的FK列可以是nullable。看似这是一个允许的模型。

它对我有用!使用可空类型如下

以下是原因周期或多个级联路径问题的解决方案:

public class Student
{
public int StudentID { get; set; }
public int FamilyID { get; set; }
  

public int? MotherID {得到;组; } //可空类型

     

public int? FatherID {get;组; } //可空类型

public string FirstName { get; set; }
public string SSN { get; set; }

public virtual Family Families { get; set; }
public virtual Mother Mothers { get; set; }
public virtual Father Fathers { get; set; }

}

最后,我的模型变为外键属性,表示可选而不是必需的关系。

非常非常感谢@ https://stackoverflow.com/users/270591/slaumahttps://stackoverflow.com/users/173937/decibyte帮助我处理此类问题。