Can I specify global mapping rules in Entity Framework Code First?
I'm building an app in ASP.NET MVC 4 using Entity Framework Code First, and for simplicity I'm inheriting all models that will be stored in the database from a BaseEntity that has a Guid, a DateCreated, a LastEditDate and a other useful properties like that. Now, I know that I can tell EF to map these inherited properties like so:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().Map(m =>
{
m.MapInheritedProperties();
});
modelBuilder.Entity<Product>().Map(m =>
{
m.MapInheritedProperties();
});
}
It seems silly to have to do this for every item, though. Is there a way I can apply this rule to all entities in one?
Such a mapping - called Table-Per-Concrete-Type (TPC) inheritance mapping - only makes sense if you really want to leverage polymorphism, for example if you want to load a list of say 10 BaseEntity
objects and expect that the actual type gets materialized so that the list contains 3 User
entities and 7 Product
entities.
Would such a query ever have any business relevance in your application? Looking at your BaseEntity
I can only see that querying all objects that - for example - have been created at a specific date, no matter which type the object has (if it's derived from BaseEntity
), could be useful. Do you need that? Also keep in mind how complex such a query would be. The SQL must query for almost all tables in your database and then union the result.
I would use inheritance mapping only if it has a real business meaning (for instance: Person
which has meaningful properties like address, phone, email, etc. on its own and Employee
that is derived from Person
and adds a Salary
and HiredDate
property, etc.).
In your case I would use the BaseEntity
only as a base type of your entity classes and don't specify any mapping at all for this class. EF will still map the inherited properties, but as part of the User
and Product
entity, etc., not as its own entity. I wouldn't even call it "Base Entity" but ... I don't know... maybe EntityBase
(meaning: the base (class) of all entities, but not an entity itself).