Questions tagged [tph]

TPH stands for Table Per Hierarchy which is a terminology that is used in Entity Framework as inheritance strategy to model the entities.

In the TPH mapping scenario, all types in an inheritance hierarchy are mapped to a single table. A discriminator column is used to identify the type of each row. When creating your model with Code First, TPH is the default strategy for the types that participate in the inheritance hierarchy. By default, the discriminator column is added to the table with the name “Discriminator” and the CLR type name of each type in the hierarchy is used for the discriminator values. You can modify the default behavior by using the fluent API.

Copied from: http://msdn.microsoft.com/en-us/data/jj591617.aspx#2.4

89 questions
1
vote
0 answers

net core 2 EF is Duplicating rows with TPH inheritance

I'm currently working on net core 2 application. I created a "Goods" abstract class. public abstract class Good : BaseEntity { public int Id { get; set; } public string Title { get; set; } public string Details { get; set; } public…
grin
  • 11
  • 3
1
vote
2 answers

Find out type with primary key given in Entity Framework using TPH

I have the following scenario: public abstract class Account { public Guid PKey { get; set; } = Guid.NewGuid(); public string Owner { get; set; } } public class CheckingAccount : Account { public int Fee { get; set; } } public…
Danghor
  • 17
  • 2
  • 7
1
vote
1 answer

Unable to create a TPH using Fluent API on EF 6

I modeling a Google Forms-like project. The bellow entities are pretty simple and straightforward (I guess), as follows. Question types: // Base class for any kind of question public abstract class Question : Bean { public string Statement {…
Yves Calaci
  • 1,019
  • 1
  • 11
  • 37
1
vote
3 answers

Entity Framework TPH changing discriminator column in an abstract base class and empty inherited class scenario

I have a table: Groups - Id - Name - Type And this table is a group of Products, Clients or Suppliers. To group products I need to use "P" in the Type column, to Group clients and suppliers, I need to use "C" and "S" respectivelly. I wonder if I…
jairhumberto
  • 535
  • 1
  • 11
  • 31
1
vote
1 answer

Sequence contains more than one matching element when adding a migration

The following code in my DbContext class causes an error when I try to add a migration protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity() .Map(m => {…
Kirsten
  • 15,730
  • 41
  • 179
  • 318
1
vote
1 answer

How to retrieve rows from a specific discriminator in EF 6.1.3?

I'm using TPH approach in my code-first model, the base class is of type WItem and the derived is BItem, I want to retrieve all the WItems rows only, so I made this return View(db.WItems.OfType().ToList()); but I still get all the rows…
Mohamed
  • 73
  • 1
  • 12
1
vote
0 answers

Entity Framework TPH: how to map foreign keys to DB columns

I am using Table-Per-Hierarchy pattern (same DB table for all derived classes), like this: Base class: public class Person { public int Id { get; set; } public string Name { get; set; } } A few derived classes, some of them having Address,…
Tomas
  • 449
  • 5
  • 11
1
vote
0 answers

TPH Inheritance and EF Code-First - Foreign Key Constraint on wrong Type

I have the following defined in a TPH schema: public abstract class Payment { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int PaymentId { get; set; } public DateTime DateOfPayment {…
RobVious
  • 12,685
  • 25
  • 99
  • 181
1
vote
0 answers

EntityFramework Migration Default Value with TPH ignored

I have the following migration definition: public override void Up() { AddColumn("dbo.MyTable", "MyEnum", c => c.Int(defaultValue: 0)); } This will add my Enum to the Sql-Table during the migration. I'm using TPH and…
1
vote
1 answer

Entity Framework Code First Two navigationProperty to/from one abstract entity

The following entities is my code first which PersonParameter is an abstract class and Shirt and Shoes are inherited from it with typ(1,2) [Table("Person")] public class Person { [Key] public int id { get; set; } public string Name {…
Hamid
  • 817
  • 1
  • 13
  • 29
1
vote
1 answer

c#, web api, swashbuckler/swagger

This is my first web api, first web app, and first time Ive used swagger via swashbuckler. I'm using TPH for my equipment db table. For example, I have base class called equipment with several child classes inherited from equipment. (This isnt real…
Czeshirecat
  • 497
  • 1
  • 3
  • 16
1
vote
0 answers

Can not querying inherited type properties in EF TPH

Work on asp.net mvc5 with ef version 6.1.3 and Unity repository .Say there is a model with a base type of SmartDevice and three derived types of SmartRainbow, SmartRouter, SmartSwitch. Like bellow: private readonly IRepositoryAsync
shamim
  • 6,640
  • 20
  • 85
  • 151
1
vote
1 answer

Define a field as protected property

I have designed an abstract class for one table namespace Test.Data { [Table("Parameter")] public abstract class Parameter { [StringLength(200)] protected string title { get; set; } protected decimal? num { get;…
Hamid
  • 817
  • 1
  • 13
  • 29
1
vote
0 answers

How to avoid Null Insert on Required TPH Columns for types that don't implement the required column

I've get a set of Class that utilize TPH inheritance. So all of the types are in the "Documents" table. Some document types have unique fields. If a sub document type has a non nullable field, the other types are getting an error when creating the…
Y Haber
  • 318
  • 1
  • 4
  • 20
1
vote
1 answer

Simple way to customize Discriminator column name

I'm using EF6 with Oracle via ODP.NET. And I need (and I can't change it) that all db objects would be in uppercase. I added some conventions and now I have all tables, columns, foreign keys etc in uppercase. All but Discriminator column that…
Alexey Merson
  • 414
  • 5
  • 12