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
2
votes
1 answer

How to customize name, length and value of a discriminator column with Entity Framework 6

I try to use EF6 with an existing database I must be compatible with. The database has been generated by nHibernate and some tables use the table-per-hierarchy (TPH) inheritance model. Discriminator columns are named Category (instead of…
daltonwide
  • 83
  • 8
2
votes
0 answers

Multiple collections with derived classes in parent entity do not work when Table-Per-Hierarchy (TPH) mapping strategy is used by EF6

I have spent better part of the weekend trying to figure this one out. I have an existing database with Subscription table that I want to map to entities in my domain using TPH inheritance strategy. There are several types of Subscription, and each…
2
votes
0 answers

Entity Framework 6, different child properties sharing the same base foreign key

I have a hierarchy of classes that is TPH in database. So we have a class Base, and children inheriting from it. Let's say they are ChildrenA, ChildrenB, ChildrenC. The Base classes hierarchy have a conceptual relation to another classes hierarchy…
John-Philip
  • 3,392
  • 2
  • 23
  • 52
2
votes
1 answer

EF Mapping Properties of an Entity Type to Multiple Tables with TPH Inheritance

I would like to use Mapping Properties of an Entity Type to Multiple Tables in the Database (Entity Splitting) whilst as the same time using Mapping the Table-Per-Hierarchy (TPH) Inheritance, therefore my model mapping code is as follows: …
user978139
  • 579
  • 4
  • 16
2
votes
0 answers

Migration from TPT to TPH in EF 6.1

Because of performance issue I need to migrate my object hierarchy from TPT to TPH structure. Is there the easiest way to migrate data from one structure to another. Object hierarchy contains around 15 classes, and I don't want to write migration…
INs
  • 2,909
  • 2
  • 20
  • 26
2
votes
0 answers

Entity Framework TPH and one to many relationships with Concrete Class

I've been working on a MVC5 website to manage insurance policies, and am stuck on what I think is a design problem. I have a code first, TPH entity situation with the following abstract class called policy: public abstract class Policy { …
Ben
  • 1,032
  • 8
  • 18
2
votes
0 answers

EF Code first TPH, doesnt update discriminator column when type is changed

I am learning the Entity framework and struggling with TPH while deleting records. I have created the POCO as follows public class Transaction { public int Id { get; set; } public DateTime TransactionDate { get; set; } …
Mahesh
  • 982
  • 8
  • 20
1
vote
0 answers

Entity Framework Multi-level TPH

Let us say that I have an Address table which can be used by two different tables. The SourceID determines which table we are joining on. This has been split out using TPH and a condition that SourceID = 1. This works great. I also want to be able…
Justin Rassier
  • 898
  • 12
  • 26
1
vote
1 answer

Retrieving data from classes that use TPH in entityframework

So I started my project using the identity scaffolding and created an application user class that inherits from identity user using Microsoft.AspNetCore.Identity; namespace test6.Models { public class ApplicationUser : IdentityUser { …
Adam
  • 11
  • 1
1
vote
1 answer

How to declare a parent child relationship when both tables are TPH and the relationship is in the base classes?

My problem relates to sales orders and sales invoices but I find it easier to think of pets and their offspring... without creating a full pedigree model. My DbContext using System; using DevExpress.ExpressApp.EFCore.Updating; using…
Kirsten
  • 15,730
  • 41
  • 179
  • 318
1
vote
0 answers

How to Mapping TPH in ownsmany

I have three classes for abstract Payment. public class CardPayment : Payment { public CardPayment(Guid distributeId, Guid invoiceId, int userId, decimal amount, DateTime dateTime, long serialNumbner, long terminalNumber, long referenceNumber,…
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
1
vote
1 answer

EF6 Table-per-Hierarchy (TPH) - abstract base class with abstract property does not generate a migration

I have an abstract base class set up for TPH: public abstract class BaseSchema { ... public abstract bool IsFilterRequired { get; set; } ... } .. and I inherit from it in other derived classes as per: public class DerivedSchema :…
Ciaran
  • 543
  • 5
  • 14
1
vote
0 answers

EF Core 2.1 Discriminated Type With Many->Many Collection

I have a model that is roughly along the following lines: Expression ExpressionGroup - Expressions (Polymorphic collection via base type that is Either Expression or ExpressionGroup) ^ Achieved by means of a base class that is configured with a…
Clint
  • 6,133
  • 2
  • 27
  • 48
1
vote
1 answer

How to query objects by entity when more than 1 entity derive from a common entity and saved in the same table

Let's say I have a parent class with 2 sub-classes, with the following configuration: modelBuilder.Entity(entity => { entity.HasDiscriminator() .HasValue("ChildA") .HasValue("ChildB"); } So how do I…
Richard77
  • 20,343
  • 46
  • 150
  • 252
1
vote
1 answer

SQL Server TPH (Table Per Hierarchy) auto increment multiple columns base on type

We currently use TPT (Table Per Type) in Entity Framework, this is very slow as we have about 20 tables, when they are queried, Entity Framework creates some massive disguising SQL which is very slow. Each table has an auto increment integer column,…
Andrew
  • 2,571
  • 2
  • 31
  • 56