With EF 4.1 Fluent Code First, you may inherit tables per type.

entity-framework entity-framework-4 entity-framework-4.1 entity-framework-6 table-per-type


I have a very simple collection of database tables, such as:


 Id (FK of Vehicle.Id)

 Id (FK of Vehicle.Id)

My class structure follows the expected pattern: Car and Bike are subclasses of Vehicle, which is an abstract class.

My EF4.1 Code First configuration is set up as follows:

class VehicleConfiguration : EntityTypeConfiguration<Vehicle> {
    public VehicleConfiguration() {
        Property(x => x.Id);
        Property(x => x.RegNo);
        HasKey(x => x.Id);

class CarConfiguration : EntityTypeConfiguration<Car> {
    public CarConfiguration() {
        Property(x => x.OtherStuff);

class BikeConfiguration : EntityTypeConfiguration<Bike> {
    public BikeConfiguration() {
        Property(x => x.MoreStuff);

However, when EF attempted to generate its model setup, I received a number of odd exceptions.

Currently, it is discarding the following:

System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'Discriminator'.

Where does it derive the name of that column from? Neither the database itself nor any of my code include it. Some convention must be seizing the initiative. How can I tell EF to utilize tables for each type?

I get a different error if I remove the "abstract" keyword from my Vehicle class (which I did as a sanity test somewhere down the road) like this:

(35,10) : error 3032: Problem in mapping fragments starting at lines 30, 35:EntityTypes AcmeCorp.Car, AcmeCorp.Bike are being mapped to the same rows in table Vehicles. Mapping conditions can be used to distinguish the rows that these types are mapped to.

There is clearly something wrong with what I'm doing, but what? I've read all the TPT + EF4.1 articles I could find, in addition to the MSDN documentation!

10/21/2013 1:30:01 AM

Popular Answer

When I encountered this issue, I found that one of my subclasses wasn't mapped. Some potential reasons in this instance are:

  1. There is another subclass, such asBus . There is no map for this.
  2. Among the subclasses, one could findCar , which is unmapped.

Make sure that each subclass is mapped in this situation:

  1. Make that the subclass has a mapping, including aToTable procedure call
  2. Make that the mapping is used whenOnModelCreating .
  3. Try using a debugger and placing breakpoints throughout all of the mapping code if you're having difficulties following this.

As an alternative, make sure that the subclass is ignored by utilizing one of the following methods:Ignore procedure calls.

10/21/2013 1:22:36 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow