I am not sure what I am doing wrong here, but I am having an issue with multiple inheritance and building my model. I get the error “The property ‘Id’ is not a declared property on type…“. Everything worked fine before I added the ContextEntity class and I had a TPC model. Each (non abstract)derived entity had it’s own ID and own table. The other classes always existed and my mappings worked fine. Here are my classes:
public abstract class Entity
{
public virtual Guid Id { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public EntityStatus EntityStatus { get; set; }
public byte[] RowVersion { get; set; }
}
public abstract class ContextEntity : Entity
{
public string Description { get; set; }
public ICollection<Comment> Comments { get; set; }
public virtual Contact Owner { get; set; }
}
public abstract class Document : ContextEntity
{
public virtual Subscription Subscription { get; set; }
}
//This is the Class I want as a table
public class Rfi : Document
{
public string Number { get; set; }
public string Subject { get; set; }
}
Before I had the ContextEntity I only had Entity. Not all my entities will use the ContextEntity. I have this mapping file:
public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
where TEntity : Entity
{
protected EntityConfiguration()
{
HasKey(e => e.Id);
Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(e => e.RowVersion).IsRowVersion();
}
}
When I just had the Entity base type it worked great. So I thought I would add another configuration mapper like this:
public class ContextEntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
where TEntity : ContextEntity
{
protected BridgeEntityConfiguration()
{
HasKey(e => e.Id);
Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(e => e.RowVersion).IsRowVersion();
HasMany(e => e.Comments).WithMany().Map(m =>
{
m.MapLeftKey("CommentId");
m.MapRightKey("EntityId");
m.ToTable("Entity_Comments");
});
HasMany(e => e.Attachments).WithMany().Map(m =>
{
m.MapLeftKey("AttachmentId");
m.MapRightKey("EntityId");
m.ToTable("Entity_Attachments");
});
}
}
My Derive mapping class looks like this:
RfiMapping: ContextEntityConfiguration<Rfi>
I am guessing EF doesn’t know what to do with all the nested base classes?
The issue was with the Subscription property in my Document class. In that class was a property that needed to be null-able. Once I added the mapping for the Subscription class and updated the null able property everything worked fine. Not sure why it was giving me an error on my Rfi class, but the issue was not in that class.