[英]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. 请注意,此处
RelatedEntity
的PrimaryEntityId
属性被显式配置为主键,因为在您的情况下,其名称不符合默认的主键命名约定。
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.