Spreading Inheritance Tree Mapping Across Assemblies in Code First
Today I helped a client to solve a problem regarding EF4.1 Code First. The issue that they banged their heads with was an inheritance tree mapping in their model which is spread across two different assemblies. When they run the application they always got the same exception – “mapping and metadata information could not be found for entitytype ‘entity name’”. So they sent me a sample solution and I checked it and gave them the solution.
The Client Provided Model
In one class library there are the following two classes:
In another console application there is another derived class for the Base class:
Also, in the same console application they have created the context:
Running the following code will produce the “mapping and metadata information could not be found for entitytype ‘entity name’” exception:
First I reproduced the exception and then tried to figure out what might be the mapping error that they get. Since the problem is in the entity type mapping then it must be caused by things like:
- Type mismatches
- Misspelled properties
- Properties that are missing
The solution to the problem is to add mapping to all the entities in the context. In order to do that you will have to override the OnModelCreating method and map all the entities:
This solves the problem and produces the relevant database when the console application is running.
It took me a short period of time in order to find the mapping problem that my client had. The solution for the problem is simple but I would expect that Code First will supply the solution within. Maybe in vNext…