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:
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. 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.
Sorry.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.