繁体   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