繁体   English   中英

用于一对一实体关系的Entity Framework 6代码第一个流利的api配置

[英]Entity Framework 6 code first fluent api config for one to one entity relationship

我目前在2个实体之间存在一对一的关系。 主要实体如下所示:

public class PrimaryEntity
{
   // primary key
   public Guid Id { get; set; }
   // some other properties...
   public RelatedEntity Related { get; set; }
}

我的相关实体如下所示:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   // some other properties
}

所以我的问题是,如何使用EF 6流利api(不能使用注释)来定义这种一对一的关系? 我为此尝试了许多不同的配置设置,但都没有运气。 甚至可以像这样定义一个单边关系,并且使键名不同吗? 我的RelatedEntity是否对主键和外键使用相同的属性?

任何帮助表示赞赏。

更新:

我能够解决问题。 罪魁祸首是我在设置Primary = new RelatedEntity的PrimaryEntity的构造函数中。 我想EF不喜欢这样。 我发现提供的所有3个答案在其他方面几乎相同。 感谢大家的帮助。

是的,您可以使用EF流利api配置一对一关系。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Configure StudentId as PK for StudentAddress
    modelBuilder.Entity<StudentAddress>()
        .HasKey(e => e.StudentId);

    // Configure StudentId as FK for StudentAddress
    modelBuilder.Entity<Student>()
                .HasOptional(s => s.StudentAddress) // Mark StudentAddress is optional for Student
                .WithRequired(ad => ad.Student); // Create inverse relationship

}

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

RelatedEntity类中,您还需要定义一个导航属性 ,该属性指向PrimaryEntity类:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}

然后,您可以使用它在自定义DbContext类的OnModelCreating方法中配置一对一关系:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}

请注意,此处RelatedEntityPrimaryEntityId属性被显式配置为主键,因为在您的情况下,其名称不符合默认的主键命名约定。

这是代码优先迁移的结果,它表明您完全可以满足需要:

CreateTable(
    "dbo.PrimaryEntities",
    c => new
        {
            Id = c.Guid(nullable: false),
        })
    .PrimaryKey(t => t.Id);

CreateTable(
    "dbo.RelatedEntities",
    c => new
        {
            PrimaryEntityId = c.Guid(nullable: false),
        })
    .PrimaryKey(t => t.PrimaryEntityId)
    .ForeignKey("dbo.PrimaryEntities", t => t.PrimaryEntityId)
    .Index(t => t.PrimaryEntityId);
public class PrimaryEntity
{
    // primary key
    public Guid Id { get; set; }
    // some other properties...
    public RelatedEntity Related { get; set; }
}
public class RelatedEntity
{
    // both the primary and foreign key
    public Guid PrimaryEntityId { get; set; }

    // some other properties
 }

 //mapping
 //configure the primary key as mentioned above
 modelBuilder.Entity<RelatedEntity>() 
      .HasKey(t => t.PrimaryEntityId );

 modelBuilder.Entity<PrimaryEntity>()
      .HasRequired(p => p.Related )
      .WithRequiredPrincipal(); //empty parenthesis for one sided navigation.

未经测试-头顶...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM