Willy
Willy

Reputation: 10652

Entity Framework code first: POCO's inheritance

I have an issue when defining some POCO objects (entities) in entity framework code first 4. I have a main entity, let's say Entity_A, which has only a property, only the ID (primary key). The rest of entities (Entity_B in this example) inherits from it (childs) except some of them (Entity_C) that inherits from another entity (from Entity_B, not from Entity_A). For example:

public class Entity_A
{
  public virtual Guid ID { get; set; }
}

// Entity_B has not primary key defined within as it inherits from Entity_A
public class Entity_B : Entity_A
{
  public virtual string propertyB1 { get; set; }
  public virtual string propertyB2 { get; set; }
  public virtual string propertyB3 { get; set; }
}

// Entity_C has not primary key defined within as it inherits from Entity_A through Entity_B
public class Entity_C : Entity_B
{
  public virtual string propertyC1 { get; set; }
  public virtual string propertyC2 { get; set; }
  public virtual string propertyC3 { get; set; }
}

so after executing it, tables for Entity_A, Entity_B, Entity_C are generated automatically but only table for Entity_A and Entity_B are correct but not for Entity_C:

Table Entity_A has fields:
-ID

which is correct.

Table Entity_B has fields:
-ID
-propertyB1
-propertyB2
-propertyB3

which is correct as well.

Table Entity_C has fields:
-ID
-propertyC1
-propertyC2
-propertyC3

which is not correct for me, as for Entity_C I expect the following fields:
-ID
-propertyB1
-propertyB2
-propertyB3
-propertyC1
-propertyC2
-propertyC3

What am I doing wrong? Is not entity framework code first (version 4.1) supporting inheritance at all?

Thanks in advance.

Upvotes: 0

Views: 485

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

Try adding a record to Entity_C, you will notice that a record is added to Entity_B and Entity_A as well.

This is table inheritance. An Entity_C is an Entity_B, so why duplicate the rows?

Upvotes: 1

Related Questions