实体框架实体有一对多和一对一? [英] Entity Framework Entity w/ One-to-Many and One-to-One?
问题描述
以下是一个父/子关系的简化示例,我试图在实体框架中首先工作。父母有一个孩子列表,其中一个被选为默认值:
The following is an simplified example of a parent/child relationship I'm trying to get working in Entity Framework code-first. The parent has a list of children, and one of them is selected as the default:
public class Parent
{
[Key]
public int Id { get; set; }
public virtual Child DefaultChild { get; set; } // One-to-One
public virtual ICollection<Child> Children { get; set; } // One-to-Many
}
public class Child
{
[Key]
public int Id { get; set; }
[Required]
public virtual Parent Parent { get; set; }
}
当我运行实体框架的 add-migration
命令,它正在将Id for Child设置为一个键,而不是使其成为 IDENTITY
,它正在使用对父级的外键引用Id。
When I run entity framework's add-migration
command, it is setting the Id for Child as a key, however, instead of making it an IDENTITY
it is using a Foreign Key reference to the parent Id.
我尝试在DbContext上执行以下操作:
I tried doing the following on my DbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>().HasOptional(p => p.DefaultChild).WithRequired(c => c.Parent);
modelBuilder.Entity<Parent>().HasMany(p => p.Children).WithRequired(c => c.Parent);
base.OnModelCreating(modelBuilder);
}
然而,它抛出了这个错误:指定的模式无效。错误:没有加载关系Sample.Presentation.DataAccess.Parent_DefaultChild,因为Sample.Presentation.DataAccess.Child类型不可用。
However, then it tosses this error: Schema specified is not valid. Errors: The relationship 'Sample.Presentation.DataAccess.Parent_DefaultChild' was not loaded because the type 'Sample.Presentation.DataAccess.Child' is not available.
你应该如何处理这些关系?
How are you supposed to handle these relationships?
我知道我可以为孩子添加一个布尔标志(例如 IsDefault
),并摆脱父对象的DefaultChild关系。但是,如果可能,我想避免这种情况。
I know I could add a boolean flag to the Child (e.g. IsDefault
) and get rid of the DefaultChild relationship on the parent. However, I'd like to avoid that if possible.
编辑:我发现这个在StackOverflow上的信息,但是使用 WithOptionalPrincipal
它在孩子上创建一个额外的ParentId字段,而不是在父项上创建ChildId?不是我想要的。
I found this info on StackOverflow utilizing WithOptionalPrincipal
, however, it creates an additional ParentId field on the child as opposed to creating the ChildId on the parent?! Not quite what I want.
编辑:添加 DatabaseGenerated
as建议在评论。这就是迁移输出:
Added DatabaseGenerated
as suggested in comments. This is what the migrations output:
CreateTable(
"dbo.Children",
c => new
{
Id = c.Int(nullable: false, identity: true),
Parent_Id = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Parents", t => t.Id) // <== I don't want this!
.ForeignKey("dbo.Parents", t => t.Parent_Id, cascadeDelete: true)
.Index(t => t.Id)
.Index(t => t.Parent_Id);
CreateTable(
"dbo.Parents",
c => new
{
Id = c.Int(nullable: false, identity: true) // <== I expect DefaultChildId after this
})
.PrimaryKey(t => t.Id); // <== and a foreign key referencing Child.Id
推荐答案
一对一和一对多关系不能在两个实体上共存。因为EF要求一对一关系的从属关键(应该有外键)的关键字必须是这个规则确保一对一的关系工作。我认为这是一个可用的解决方案来添加一个属性IsDefault到子实体。
An one-to-one and an one-to-many relationship can not coexist on two entities.Because EF requires the key of the dependent end(should have foreign key) of an one-to-one relationship must be the foreign key to the principal end.This rule makes sure an one-to-one relationship work.I think it's an available solution to add a property,IsDefault,to the Child entity.
这篇关于实体框架实体有一对多和一对一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!