2017-07-25 52 views
0

我有一个具有多个属性的类,可以将其映射到属性不同的一个表中。 我这样做没有为每个属性具有相同模式的几个表。 如下:EF映射多个属性相同的表。关系''未加载,因为类型''不可用

public class Flower : Entity 
    { 
     public Flower() 
     { 
      Events = new Collection<Event>(); 
     } 

     public Guid Id { get; set; } 

     public virtual User User { get; set; } 

     public FlowerType Type { get; set; } 

     public virtual ExtraInfoBase Family { get; set; } 

     public virtual ExtraInfoBase Specie { get; set; } 

     public virtual ExtraInfoBase Seller { get; set; } 

     public string SellerObs { get; set; } 

     public virtual ExtraInfoBase Localization { get; set; } 

Extrainfobase是一类能够处理与diferent类型的所有这些属性:

public class ExtraInfoBase 
{ 
    public int Id { get; set; } 
    public InfoType Type { get; set; } 
    public string Value { get; set; } 
    public string Extra { get; set; } 
    public virtual Flower Flower { get; set; } 
    public virtual User User { get; set; } 
} 

映射如下:

public class FlowerMap : EntityTypeConfiguration<Flower> 
{ 
    public FlowerMap() 
    { 
     this.HasKey(t => t.Id); 

     this.ToTable("Flowers"); 

     this.Property(t => t.Id) 
      .HasColumnName("Id") 
      .HasColumnType("uniqueidentifier"); 

     this.Property(t => t.Type) 
      .IsRequired() 
      .HasColumnName("Type"); 

     this.HasRequired(t => t.Family).WithRequiredPrincipal(x => x.Flower); 

     this.HasRequired(t => t.Gender).WithRequiredPrincipal(x => x.Flower); 

     this.HasRequired(t => t.Specie).WithRequiredPrincipal(x => x.Flower);` 

public class ExtraInfoBaseMap : EntityTypeConfiguration<ExtraInfoBase> 
{ 
    public ExtraInfoBaseMap() 
    { 
     this.HasKey(t => t.Id); 

     this.ToTable("ExtraInfo"); 

     this.Property(t => t.Id) 
      .HasColumnName("Id") 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 

     this.Property(t => t.Type) 
      .IsRequired() 
      .HasColumnName("Type"); 

     this.Property(t => t.Value) 
      .IsRequired() 
      .HasColumnName("Value"); 

     this.Property(t => t.Extra) 
      .HasColumnName("Extra"); 

     this.HasRequired(x => x.Flower); 
     this.HasRequired(x => x.User); 
    } 
} 

,我发现了错误关系“Flower_Family”未加载,因为类型“ExtraInfoBase”不可用。 我在做什么错? 请指教。

+0

如果'ExtraInfoBase'继承自'Entity',它会工作吗? – mjwills

+0

另请在你的文章中加入'User'类。 – mjwills

回答

0

对不起,浪费你的时间。昨天晚上我非常疲倦,睡眠中找到了一种可能的解决方案。这种关系是错误的。 Extrainfobase和花将有多对多的关系,所以模型是错误的。

对不起。

相关问题