1

I'm having an issue with discriminators in TPH inheritance with Entity Framework v6.1.1

I would expect that the discriminator should fall on the table that is representative of the base class. However, it appears EF is trying to map the discriminator column to the table mapped to the derived class

E.g.

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Staff : Person
{
    public decimal? Salary { get; set; }
}

public class MyContext : DbContext
{
    public MyContext()
        : base("MyConnectionString") {}

    public virtual IDbSet<Person> Persons { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>()
            .Map(x => x.ToTable("Person"))
            .Map<Staff>(x => x.ToTable("Staff").Requires("PersonTypeId").HasValue(1));
    }
}

I am also using an existing schema too - i.e:

CREATE TABLE Person
(
    Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    Name VARCHAR(50) NULL,
    PersonTypeId INT NOT NULL
)

CREATE TABLE Staff 
(
    Id INT NOT NULL REFERENCES Person(Id),
    Salary DECIMAL NULL
)
GO

However, when I try to add a new Staff, I encounter the following exception:

'System.Data.SqlClient.SqlException : Cannot insert the value NULL into column 'PersonTypeId', table 'MyDb.dbo.Person'; column does not allow nulls. INSERT fails. The statement has been terminated'

It appears that it is trying to insert the discriminator (incorrectly) to the derived table. Hope someone can help.

pes502
  • 1,597
  • 3
  • 17
  • 32
Nagoh
  • 813
  • 2
  • 10
  • 23
  • 1
    sounds weird to use several tables for a TablePerHierarchy <=> one table. Are you sure you're no trying TPT and in this cas there is no need for discriminator. – tschmit007 Jul 01 '14 at 07:48
  • Sorry - just used this as an example. Imagine if you will that it will span across more types (eg: Student) – Nagoh Jul 01 '14 at 07:50
  • same remarks for any number of types: in TPH you use only one table for the whole hierarchy. [http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph] – tschmit007 Jul 01 '14 at 07:53
  • Ah - right you are - I appear to have my wires crossed around TPT / TPH. No need for a discriminator. Thanks for your help – Nagoh Jul 01 '14 at 08:15

1 Answers1

0

So it appears that I've mis-understood the TPH setup for Entity Framework.

In my case, I'm wanting to map derived types to separate tables, which is an example of Table per Type - http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt

Discriminators are redundant in this context.

Nagoh
  • 813
  • 2
  • 10
  • 23