Family、Mother、Father、Student の 4 つのモデルがあります。Father& Mother& Student を Family テーブルに、Student テーブルを Mother テーブルと Father テーブルに関連付けようとしています。ここで、Family テーブルには、ファミリに関する情報が含まれています。Mother テーブルと Father テーブルには個人情報が含まれ、Student テーブルには生徒情報 + MotherID と FatherID が含まれます。つまり、Family テーブルは、Mother と Father の Primary テーブルであり、Student テーブルでした。MotherテーブルのMotherIDとFatherテーブルのFatherIDはStudentテーブルを参照していました。
私は MVC 4 Entity Framework Code First を使用しています。
これらは私のモデルクラスです。
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.
私は何を間違っていますか??
なぜこのエラーが発生するのですか??? 誰か助けてください!
最初にEFコードを使用して1対多および多対多の関係を実装する正しい方法は何ですか? この例で私を導いてください。