Share via

Table-per-hierarchy inheritance

Vojislav Ćeklić 1 Reputation point
2021-08-04T05:38:32.283+00:00

Hi there,

I have created inheritence

public class Item
{
int Id;
string SerialNumber;
......

}
public class Phone : Item
{
Model PhoneModel;
string ScreenSize;
......
}
public class Sim : Item
{
string PhoneNumber;
.......
}

What is the best way to get Items with all properties for both Phone and Sim?

If I create entity that will hold all items (something like shopping cart) what is the best way to distinguish what is type od item. Thanks

Developer technologies | .NET | Entity Framework Core
0 comments No comments

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,661 Reputation points
    2021-08-05T02:38:58.67+00:00

    Hi Vojislavekli-7484,
    >>What is the best way to get Items with all properties for both Phone and Sim?
    Query basic entities(Item) will get the results of all entities in the hierarchy.
    If I misunderstood what you mean, please explain in detail.
    >>what is the best way to distinguish what is type od item.
    TPH uses a single table to store the data for all types in the hierarchy, and a discriminator column is used to identify which type each row represents.
    The discriminator can be configured to distinguish different types.
    More details please refer to this document.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.