[英]Entity Framework 6 configure model using Fluent API
I have hierarchical model (binary tree). 我有层次模型(二叉树)。
class Partner
{
public int ID {get; set; }
public string Name { get; set; }
// Parent in the tree.
public virtual Partner BinarParent {get; set;}
// Who is invited partner in tree.
public virtual Partner Sponsor {get; set;}
// Childs partners.
public virtual List<Partner> Childs {get; set;}
}
I am having exception now: 我现在有例外:
Unable to determine the principal end of an association between the types 'Partner' and 'Partner'. 无法确定类型“伙伴”和“伙伴”之间的关联的主要终点。 The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
必须使用关系流利的API或数据注释显式配置此关联的主要端。
How can i configure navigation properties (Sponsor and Binary Parent) using Fluent API, for this model? 如何为此模型使用Fluent API配置导航属性(Sponsor和Binary Parent)?
PS 聚苯乙烯
Help me, please. 请帮帮我。
Do this in your Context: 在您的上下文中执行此操作:
public class YourContext : DbContext
{
//...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure the primary key for the Partner
modelBuilder.Entity<Partner>().HasKey(t => t.ID);
// Map one-to-many relationship
modelBuilder.Entity<Partner>()
.HasMany(p => p.Childs)
.WithOptional(p => p.BinarParent);
modelBuilder.Entity<Partner>()
.HasOptional(t => t.Sponsor)
.WithOptionalDependent()
.Map(t => t.MapKey("FK_Sponsor_Id"))
}
//...
}
The first relationship maps your BinarParent Property with your Collection of Childs. 第一个关系将BinarParent属性与您的Childs集合映射。 The second relationship map the property Sponsor.
第二个关系映射属性“赞助者”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.