EF Code-First 學習之旅 一對多的關係


public
class Student { public Student() { } public int StudentId { get; set; } public string StudentName { get; set; } public virtual Standard Standard { get; set; } } public class Standard { public Standard() { Students = new List<Student>(); } public int StandardId { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } }

 

上面的代碼中,Student實體包含導航屬性Standard,Standard實體包含集合導航屬性Student,Code First的默認規則爲1對多的關係ide

 

 

 指定外鍵ui

public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

        public int StdandardRefId { get; set; }
        
    [ForeignKey("StandardRefId")]
    public virtual Standard Standard { get; set; }
}
       
public class Standard
{
    public Standard()
    {
        Students = new List<Student>();
    }
    public int StandardId { get; set; }
    public string Description { get; set; }

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

 

 

 

 

Fluent API配置spa

public class Student
{
    public Student(){ }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

    public int StandardId { get; set; }

    public virtual Standard Standard { get; set; }
}
       
public class Standard
{
    public Standard()
    {
        StudentsList = new List<Student>();
    }
    public int StandardId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        //one-to-many 
        modelBuilder.Entity<Student>()
                    .HasRequired<Standard>(s => s.Standard) // Student entity requires Standard 
                    .WithMany(s => s.Students); // Standard entity includes many Students entities

}

 

 

 若是外鍵不符合默認規則3d

public class Student
{
    public Student(){ }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

    //StdId is not following code first conventions name
    public int StdId { get; set; }

    public virtual Standard Standard { get; set; }
}
       
public class Standard
{
    public Standard()
    {
        StudentsList = new List<Student>();
    }
    public int StandardId { get; set; }
    public string Description { get; set; }

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

 

能夠以下配置code

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        //one-to-many 
        modelBuilder.Entity<Student>()
                    .HasRequired<Standard>(s => s.Standard)
                    .WithMany(s => s.Students)
                    .HasForeignKey(s => s.StdId);

}

modelBuilder.Entity<Student>().HasRequired<Standard>(s => s.Standard)blog

代表Student必須包含Standard導航屬性,ip

.WithMany(s => s.Students).HasForeignKey(s => s.StdId)代表Standard有多個Student,外鍵名爲StdId。ci

另外一種方法get

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        //configure one-to-many
        modelBuilder.Entity<Standard>()
                    .HasMany<Student>(s => s.Students) Standard has many Students
                    .WithRequired(s => s.Standard)  Student require one Standard
                    .HasForeignKey(s => s.StdId);Student includes specified foreignkey property name for Standard
}

 

外鍵可空的配置string

 

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        //one-to-many 
        modelBuilder.Entity<Student>()
                    .HasOptional<Standard>(s => s.Standard)
                    .WithMany(s => s.Students)
                    .HasForeignKey(s => s.StdId);

}
   

 

 

配置如發生以下異常

One or more validation errors were detected during model generation:

Domain.Student_Standard: : Multiplicity conflicts with the referential constraint in Role 'Student_Standard_Target' in relationship 'Student_Standard'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

 

則須要配置外鍵爲nullable類型

相關文章
相關標籤/搜索