我有以下课程
Class A ()
{
public virtual ThisClass thisClass { get; set; }
}
Class B ()
{
public virtual ThisClass thisClass { get; set; }
}
Class C ()
{
public virtual ThisClass thisClass { get; set; }
}
Class D ()
{
public virtual ThisOne thisClass { get; set; }
}
//
Class ThisClass
{
[ForeignKey("A")]
[ForeignKey("B")]
[ForeignKey("C")]
[ForeignKey("D")]
Public int Id {get;set;}
}
我不知道如何使用实体框架分配这些外键,有什么办法呢?
网站需要更多关于我的文字,所以我写这一行,请忽略这一行。谢谢
答案 0 :(得分:0)
你的课就像:
Class ThisClass
{
Public int Id {get;set;}
public virtual A a { get; set; }
public virtual B b { get; set; }
public virtual C c { get; set; }
public virtual D d{ get; set; }
}
可以使用流畅的API以这种方式进行一对一映射:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<A >()
.HasKey(e => e.Id );
modelBuilder.Entity<B >()
.HasKey(e => e.Id );
modelBuilder.Entity<C >()
.HasKey(e => e.Id );
modelBuilder.Entity<D >()
.HasKey(e => e.Id );
modelBuilder.Entity<ThisClass >()
.HasRequired(s => s.A)
.WithRequiredPrincipal(ad => ad.thisClass);
modelBuilder.Entity<ThisClass >()
.HasRequired(s => s.B)
.WithRequiredPrincipal(ad => ad.thisClass);
modelBuilder.Entity<ThisClass >()
.HasRequired(s => s.C)
.WithRequiredPrincipal(ad => ad.thisClass);
modelBuilder.Entity<ThisClass >()
.HasRequired(s => s.D)
.WithRequiredPrincipal(ad => ad.thisClass);
}