实体框架代码首先配置一对多

时间:2016-11-23 15:52:28

标签: c# .net entity-framework

我开始使用并熟悉实体代码第一关系

  

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

我在使用EntityTypeConfiguration

将以下情况转换为代码时遇到了困难

Family( Id pk int,
        Name nvarchar)
Child( Id pk int, 
       FamilyId NULL constraint FK_Child_Family FOREIGN KEY REFERENCES Family (Id) DEFAULT NULL)

class Family
{
     prop Id;
     prop Name;
}

class Child
{
     prop Id;

     prop FamilyId;
     virtual prop Family
}

我有家庭配置

private void Configure(EntityTypeConfiguration<Family> entity)
        {
            entity.ToTable(nameof(Family));

            entity.HasKey(key => key.Id).Property(key => key.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); ///--> primary key configuration

            entity.Property(prop => prop.Name).IsRequired();
        }

我正在努力实现以下目标

家庭不了解儿童

孩子知道家庭并且有FamilyId(FK)

这是我目前为儿童配置

所拥有的
private void Configure(EntityTypeConfiguration<Child> entity)
        {
             ///--> only Child contains a Family FK 
             ///--> need to map to FamilyId
             ///--> want to specify name of FK : "FK_Child_Family"
           entity.HasOptional(user => user.Family) .... ? 

        }

谢谢

0 个答案:

没有答案