简体   繁体   English

EF映射同一表的多个属性。 未加载关系“”,因为类型“”不可用

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

I have a class with multiple properties that can be mapped to one table with a different type as property. 我有一个具有多个属性的类,这些类可以映射到一个具有不同类型的表作为属性。 I did this to don't have several tables with the same schema for each property. 我这样做是为了使每个属性没有几个具有相同架构的表。 As follows: 如下:

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 is a class that can handle all those properties with a diferent type: 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; }
}

The mapping as follows: 映射如下:

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

I'm getting the error The relationship 'Flower_Family' was not loaded because the type 'ExtraInfoBase' is not available. 我收到错误,因为类型'ExtraInfoBase'不可用,所以未加载关系'Flower_Family'。 What am i doing wrong? 我究竟做错了什么? Please advise. 请指教。

Sorry to waste your time. 抱歉浪费您的时间。 I was very tired last night and a possible solution came to me in the sleep. 昨晚我非常疲倦,一个可能的解决方案在睡眠中向我传来。 The relations are wrong. 关系是错误的。 Extrainfobase and flower will have a many to many relation so the model is wrong. Extrainfobase和flower将具有多对多关系,因此该模型是错误的。

Sorry. 抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未加载FK关系,因为该类型不可用 - The FK relationship was not loaded because the type is not available 由于类型xx不可用,因此未加载关系xx - The relationship xx was not loaded because the type xx is not available 指定的EF6模式无效。 错误:由于类型y不可用,所以未加载关系x - EF6 Schema specified is not valid. Errors: The relationship x was not loaded because the type y is not available 指定的架构无效。 错误:未加载关系,因为类型不可用 - Schema specified is not valid. Errors: The relationship was not loaded because the type is not available 未加载关系,因为在托管网站时类型不可用错误 - The relationship was not loaded because the type is not available error while hosting website EF引用同一类型的多个属性 - EF multiple properties referencing same type EF CORE 映射到同一个表的多个属性 - EF CORE multiple properties that map to the same table 具有TPH继承的实体类型到多个表的EF映射属性 - EF Mapping Properties of an Entity Type to Multiple Tables with TPH Inheritance EF 6:插入多个-无法更改关系,因为一个或多个外键属性不可为空 - EF 6: inserting multiple - The relationship could not be changed because one or more of the foreign-key properties is non-nullable 同一主键ApplicationUser表上的EF多个外键关系 - EF multiple foreign key relationship on same primary key ApplicationUser table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM