Entity Framework 6 code first fluent 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 fluent api(不能使用注释)来定义这种一对一关系?为此,我尝试了许多不同的配置设置变体,但都没有成功。是否有可能像这样定义一种单向关系,并且键名不同?我的 RelatedEntity 是否对主键和外键使用相同的 属性?
感谢任何帮助。
更新:
我能够解决问题。罪魁祸首是在我设置 Related = new RelatedEntity 的 PrimaryEntity 的构造函数中。我想 EF 不喜欢这样。我发现提供的所有 3 个答案在其他方面几乎相同。感谢大家的帮助。
是的,您可以配置与 EF fluent 的一对一关系 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
}
在RelatedEntity
class中你还需要定义一个导航属性,它指向PrimaryEntity
class:
public class RelatedEntity
{
// both the primary and foreign key
public Guid PrimaryEntityId { get; set; }
public PrimaryEntity Primary { get; set; }
// some other properties
}
然后您可以使用它来配置您自定义 OnModelCreating
方法中的一对一关系 DbContext
class:
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);
}
请注意,此处 RelatedEntity
的 PrimaryEntityId
属性 已明确配置为主键 因为它的名称在您的案例中是超出默认的主键命名约定。
这是代码优先迁移的结果,它表明您得到了您所需要的:
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.
未测试 - 超出我的想象...
我目前有 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 fluent api(不能使用注释)来定义这种一对一关系?为此,我尝试了许多不同的配置设置变体,但都没有成功。是否有可能像这样定义一种单向关系,并且键名不同?我的 RelatedEntity 是否对主键和外键使用相同的 属性?
感谢任何帮助。
更新:
我能够解决问题。罪魁祸首是在我设置 Related = new RelatedEntity 的 PrimaryEntity 的构造函数中。我想 EF 不喜欢这样。我发现提供的所有 3 个答案在其他方面几乎相同。感谢大家的帮助。
是的,您可以配置与 EF fluent 的一对一关系 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
}
在RelatedEntity
class中你还需要定义一个导航属性,它指向PrimaryEntity
class:
public class RelatedEntity
{
// both the primary and foreign key
public Guid PrimaryEntityId { get; set; }
public PrimaryEntity Primary { get; set; }
// some other properties
}
然后您可以使用它来配置您自定义 OnModelCreating
方法中的一对一关系 DbContext
class:
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);
}
请注意,此处 RelatedEntity
的 PrimaryEntityId
属性 已明确配置为主键 因为它的名称在您的案例中是超出默认的主键命名约定。
这是代码优先迁移的结果,它表明您得到了您所需要的:
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.
未测试 - 超出我的想象...