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
21
votes
2 answers

ef-core load collection property of nested tph inherited member

Given the following class structure public class Parent { public Guid Id { get; public List Children { get; set; } } public abstract class BaseChild { public int Id { get; set; } public…
Elmer Ortega
  • 469
  • 4
  • 12
8
votes
2 answers

Farther extending ApplicationUser class in ASP.NET MVC5 Identity system

I'm creating a simple application for university where a student can make some type of request which is then processed by an employee of particular speciality. I would like to use default MVC5 identity system and to extend the ApplicationUser class…
7
votes
2 answers

EF TPH Inheritance Query

Trying to implement a very simple TPH setup for a system I'm making, 1 base, 2 inherited classes. However the inherited classes all belong to the same entity set, so within my ObjectContext using loop, I can only access the base abstract class. I'm…
Psytronic
  • 6,043
  • 5
  • 37
  • 56
4
votes
1 answer

How to persist columns as json using the table-per-hierarchy (TPH) pattern in Entity Framework Core 2

I am trying to store a table using the table-per-hierarchy pattern but instead of columns for each derived field, I want to store it as json. I am simply doing the example from the inheritance section in the .net entity framework core documentation…
Jonas Lomholdt
  • 1,215
  • 14
  • 23
4
votes
4 answers

Using EF Include on TPH

I have implemented code first db architecture with simple inheritance using THP: And I need to query all notifications of all type. TargetUser property in NotificationUser table is a association. I'm trying to execute next code: var notifications…
Ivvan
  • 720
  • 11
  • 25
4
votes
1 answer

Multiple levels of inheritance with Entity Framework using TPH

I am using Entity Framework to create a Table Per Heirachy data model in a new project over an existing legacy database. This means that we don't own the database and therefore I need a solution that doesn't involve adding columns to the…
Richiban
  • 5,569
  • 3
  • 30
  • 42
4
votes
0 answers

Using Entity Framework 6 to implement nested TPH on 2 different discriminator fields

I'm working with EF6, migrating from edmx to Code first. I did a reverse engineering from an existing DB, and I'm tring to reproduce the model I had before with a nested TPH done on a table called "Contacts" wich has 2 different fields "Type" and…
ilFusta
  • 103
  • 9
4
votes
1 answer

TPH and Many to Many relationships

Hi I'm using TPH inheritance to establish similar configurable items. My TPH Model is: public class baseConfigItem { public int ID {get; set;} public string ExternalText {get; set;} public string InternalText { get; set;…
user3836415
  • 952
  • 1
  • 10
  • 25
4
votes
0 answers

Can I and should I create one or many views and controllesr for TPH inheritance hierarchy in ASP.NET-MVC

I've read plenty of articles about inheritance with Entity Framework(in ASP.NET-MVC context too), everybody writes about database's side of the problem, but nobody brings up view side of the problem. We got model classes: public class Person { …
Yoda
  • 17,363
  • 67
  • 204
  • 344
3
votes
2 answers

asp.net core identity Multiple users from BaseApplicationUser

I am using ASP.NET Core 2.2 Identity for my user management system. I need to have several types of users ... for example, warehouse user and application user, which I create a base class that inherits from identity user class =>…
3
votes
1 answer

Derived types generating their own ForeignKey field in code first using TPH

I'm using EF Core and implementing inheritance in a table-per-hierarchy (TPH) approach. I've found that once I added the hierarchy objects to my data model the migration is adding a foreign key for the base table and what appears to be a duplicate…
Steve
  • 279
  • 3
  • 13
3
votes
0 answers

Using Web API ODATA $expand=* for TPH entities

The ODATA $expand=* term expands all properties of an entity. But in the TPH pattern, it expands only the base class reference properties: public abstract class Person { [Key] public int Id {get; set; } public Place BirthPlace { get;…
Khodaie
  • 346
  • 4
  • 17
3
votes
1 answer

Why does EntityFramework 6 not support explicitly filtering by Discriminator?

The following is a small EF6 Program to demonstrate the issue. public abstract class Base { public int Id { get; set; } public abstract int TypeId { get; } } public class SubA : Base { public override int TypeId => 1; } public class…
default0
  • 95
  • 5
2
votes
1 answer

Mapping reference property to abstract parent

I have a complex object hierarchy in an enterprise application. I'll try and keep it simple, and abstract, yet still representative of what I'm dealing with. My project deals with several styles of the same type of object. For this, we have…
krillgar
  • 12,596
  • 6
  • 50
  • 86
2
votes
1 answer

Entity Framework many-to-many With additional column

I have two classes: public class Address { public Guid Id { get; set; } public virtual ICollection MessagesTo { get; set; } public virtual ICollection MessagesCopyTo { get; set; } } public…
ALex Qz
  • 21
  • 1
1
2 3 4 5 6