簡體   English   中英

EF映射同一表的多個屬性。 未加載關系“”,因為類型“”不可用

[英]EF Mapping multiple properties same table. The relationship '' was not loaded because the type '' is not available

我有一個具有多個屬性的類,這些類可以映射到一個具有不同類型的表作為屬性。 我這樣做是為了使每個屬性沒有幾個具有相同架構的表。 如下:

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是一個類,可以處理具有不同類型的所有那些屬性:

 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);
    }
}

我收到錯誤,因為類型'ExtraInfoBase'不可用,所以未加載關系'Flower_Family'。 我究竟做錯了什么? 請指教。

抱歉浪費您的時間。 昨晚我非常疲倦,一個可能的解決方案在睡眠中向我傳來。 關系是錯誤的。 Extrainfobase和flower將具有多對多關系,因此該模型是錯誤的。

抱歉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM