question

Vojislavekli-7484 avatar image
0 Votes"
Vojislavekli-7484 asked Vojislavekli-7484 commented

Table-per-hierarchy inheritance

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

dotnet-entity-framework-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered Vojislavekli-7484 commented

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.


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your answer. You are right, it returns them all, but just properities defined in basic entery (item) not in derived ones. I would like to have list of all items with persepctive properties, for i.e.:

ITEMS

ID Serial Number Screen Size Phone Number ....................... Action
1 1234 5" Null Borrow
2 009ABC Null 416-989-5554 Borrow
3 1233 6" Null Borrow
4 08A555 Null 416-998-555 Borrow

Idea is to have this list, so I can click "Borrow" and put this item in Table that holds history of borrowed items (regardles of type)
I hope this is a bit clearer for you.

For second part I learnt that I could use : var p = Item.GetType() or if(item is Phone)

Thanks a million.

0 Votes 0 ·

Hi @Vojislavekli-7484,
You can try the Polymorphic Queries which queries all instances of a class and its subclasses respectively.
Code looks like:

 public class EntityContext : DbContext
 {
     public DbSet<Item> items { get; set; }
 }
 var query = context.items.ToString();

More details you can refer to this document.
Best Regards,
Daniel Zhang


0 Votes 0 ·

Thanks DanielZhang-MSFT,

I do not have context.items.ToString();, there are just .ToList(), ToArray(), ToDictionary(), ToLookup, ToHashSet......no ToString method on this DBSet.

I tought I could do DTO, that I can manually populate, since I just need this for views.
I will try something.

0 Votes 0 ·
Show more comments