【译】第26节---配置一对多关系

原文:http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

本节,我们将学习如何在Code First中配置两个实体之间的一对多关系。

举一个Student和Standard(年级)实体的例子,其中,一个Standard可以包括许多Student。 所以Student与Standard实体之间的关系将是一对多的。

 

使用DataAnnotation配置一对多关系

来看下面Student与Standard实体:

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的集合属性。 这是形成一对多关系的默认约定。

如果实体类遵循此约定,则我们不需要使用DataAnnotations或Fluent API配置一对多关系。

EF Code First将通过在Student表中添加Standard_StandardId列来创建一对多关系,如下所示:

 

实体包括外键Id属性:

建议在实体类中包含外键属性。 例如,Student实体包含自动变为foreignkey属性的StandardId属性,因为它符合外键<Type Name> Id的约定。

如果foreignkey属性名称不符合约定,例如,Student实体对于Standard实体使用了不同名称的外键(不是StandardId),那么我们需要在属性上应用ForeignKey属性。

例如,以下Student实体包括StandardRefId属性:

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; }
}

上述示例中,ForeignKey属性应用于Standard导航属性以指定Standard属性的foreignkey属性名称。所以EF将创建一个StandardRefId列作为FK,如下所示:

 

使用Fluent API配置一对多关系

这里,我们将学习使用Fluent API的Student和Standard实体之间的一对多关系。

让我们为以下Student和Standard实体配置一对多关系:

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

}

假设Student和Standard实体类没有遵循外键的Code First约定:

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; }
}

所以,你可以通过Fluent API使用Student实体类配置一对多关系,如下所示:

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)指定Student实体需要NotNull标准导航属性。

.WithMany(s => s.Students).HasForeignKey(s => s.StdId)指定Standard实体可以包括多个Student在学生集合属性中,外键是StdId。

另一种可行的方式:我们也可以从Standard实体开始:

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
}

 上面的代码将创建以下数据库:

注意StdId不为空列。 所以每次添加或更新Student时,都必须为Student实体指定Standard。

 

一对多关系中的可空外键:

使用HasOptional方法代替HasRequired方法使外键列为空。

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);

}

 

下节学习多对多关系的配置。

转载于:https://www.cnblogs.com/talentzemin/p/7284923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值