Can I specify global mapping rules in Entity Framework Code First?

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).

Leave a Comment