J'ai une classe avec plusieurs propriétés qui peuvent être mappées à une table avec un type différent en tant que propriété. Je l'ai fait pour ne pas avoir plusieurs tables avec le même schéma pour chaque propriété. comme suit:EF Mappage de plusieurs propriétés dans la même table. La relation '' n'a pas été chargée car le type '' n'est pas disponible
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 est une classe qui peut gérer toutes ces propriétés avec un type 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; }
}
La mise en correspondance comme suit:
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);
}
}
Je reçois le error La relation 'Flower_Family' n'a pas été chargée car le type 'ExtraInfoBase' n'est pas disponible. Qu'est-ce que je fais de mal? S'il vous plaît aviser.
Cela fonctionne-t-il si 'ExtraInfoBase' hérite de' Entity'? – mjwills
Veuillez également inclure la classe 'User' dans votre message. – mjwills