I'm trying to implement common logic for database entity classes, so I'd like to introduce a common, not mapped, abstract base class for my entities like this.
public abstract class BaseEntity {
public int Id { get; set; }
public byte[] RowVersion { get; set; }
}
I don't want to represent this base class in the database, but only at a logical level in the entity model. The project is a Database-First project, having an EDMX model. If it was Code-First, I could (I guess) easily achieve this by annotating the base class properties with the proper EF annotations (Key
, etc.).
But in the EDMX it seems like I'm unable to select a custom base type which is not an entity itself. When I tried to add a new entity, it complained about the fact that it's not mapped.
I've also tried using an interface like IDbEntity
having the above 2 properties, but when I write a generic method like the following (just quick example), I get error about the interface property not being mapped. Seems like EF cannot recognize that basically the Id
access corresponds to the unerlying entity type.
public static TDbEntity[] GetByIds<TDbEntity>(this IQueryable<TDbEntity> queryable, int[] ids)
where TDbEntity : IDbEntity
{
return queryable.Where(e => ids.Contain(e.Id)).ToArray();
}
I simply cannot find a viable option for this kind of base class when using Database-First. Any help would be appreciated.