简体   繁体   English

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

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

I currently have a one-to-one relationship between 2 entities. 我目前在2个实体之间存在一对一的关系。 The primary entity looks like this: 主要实体如下所示:

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

And my related entity looks like this: 我的相关实体如下所示:

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

   // some other properties
}

So my question is, how could this one-to-one relationship be defined using the EF 6 fluent api (can't use annotations)? 所以我的问题是,如何使用EF 6流利api(不能使用注释)来定义这种一对一的关系? I've tried many different variations of config settings for this, all with no luck. 我为此尝试了许多不同的配置设置,但都没有运气。 Is it even possible to define a one sided relationship like this, and have the key names different? 甚至可以像这样定义一个单边关系,并且使键名不同吗? Is the fact that my RelatedEntity is using the same property for both primary and foreign keys? 我的RelatedEntity是否对主键和外键使用相同的属性?

Any help is appreciated. 任何帮助表示赞赏。

UPDATE: 更新:

I was able to resolve the issue. 我能够解决问题。 The culprit was in the constructor of the PrimaryEntity I was setting Related = new RelatedEntity. 罪魁祸首是我在设置Primary = new RelatedEntity的PrimaryEntity的构造函数中。 I guess EF doesn't like this. 我想EF不喜欢这样。 I found that all 3 answers provided worked pretty much the same otherwise. 我发现提供的所有3个答案在其他方面几乎相同。 Thanks for everyone's help. 感谢大家的帮助。

Yes you can configure a one-to-one relationship with EF fluent api. 是的,您可以使用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

}

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

In RelatedEntity class you also need to define a navigation property , which points to PrimaryEntity class: RelatedEntity类中,您还需要定义一个导航属性 ,该属性指向PrimaryEntity类:

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

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

Then you may use it to configure your one-to-one relationship in OnModelCreating method of your custom DbContext class: 然后,您可以使用它在自定义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);
}

Note that here RelatedEntity 's PrimaryEntityId property is explicitly configured to be a primary key because it's name in your case is out of default primary key naming convention. 请注意,此处RelatedEntityPrimaryEntityId属性被显式配置为主键,因为在您的情况下,其名称不符合默认的主键命名约定。

And here is a resulting code first migration, which shows that you get exactly what you need: 这是代码优先迁移的结果,它表明您完全可以满足需要:

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.

Not tested - off the top of my head... 未经测试-头顶...

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

相关问题 与Entity Framework Fluent API的一对一关系 - One to one relationship with Entity Framework Fluent API 实体框架代码First Fluent API配置,用于一对一的识别关系 - Entity Framework Code First Fluent API configuration for one to one identifying relationship 实体框架代码优先的一对一关系 - One to One relationship in Entity Framework Code First 使用 Entity Framework Fluent API 的一对一可选关系 - One to one optional relationship using Entity Framework Fluent API 实体框架通过流畅的API继承了一对零的一对一关系配置 - Entity Framework inherited one to zero to one relationship configuration with fluent API 使用Fluent API在实体框架中创建一对多关系 - Creating one to many relationship in Entity Framework using Fluent API 在实体框架6流畅的api中的视图上映射多对一关系 - Mapping a many to one relationship over a view in entity framework 6 fluent api 实体框架代码优先方法一对一流利的api映射 - Entity Framework code-first approach one-to-one fluent api mapping 使用Entity Framework Code First和Fluent API配置许多一对一关系 - Configuring many One-To-One relationships with Entity Framework Code First and Fluent API Entity Framework 5.0代码首先是一对一和一对多的关系 - Entity Framework 5.0 code first one to one and one to many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM