0

I'm currently trying to figure out how to map current database ERD to a C# class relationship using Fluent API. I am familiar with basics of EF Core but can't wrap my head around this problem. This is an example of my ERD:

| tbl_elements  |
|---------------|
| element_id    |
| last_modified |
| tbl_poles     |
|---------------|
| pole_id       |
| color         |
| height        |

| tbl_towers    |
|---------------|
| tower_id      |
| color         |
| type          |

I want to map them like this: element_id : pole_id and element_id : tower_id.

However when I use this approach One-To-One I get a foreign key in both tbl_poles and tbl_towers and I don't want this. I also tried using Inheritance but then I can't use tbl_elements anymore but I need al the tables in my database. I searched the internet for solution and tried al of the provided solutions but didn't found any. I found this: solution, but like I said I need the parent class to be a separate table and not merged.

To give more context: I'm trying to read a shapefile.shp and extract al the features and put them in the corresponding tables.

dtkturpal
  • 21
  • 4

1 Answers1

0

Until the .NET 5 EF Core has only supported Table-Per-Hierarchy inheritance model. But now it supports also Table-Per-Type and it is pretty easy to configure.

Check the Microsoft Docs.

kmacek
  • 51
  • 6
  • Thank you very much, I saw this solution but I was indeed trying to implement Table-Per-Hierarchy instead of Table-Per-Type. This solved my problem. – dtkturpal Mar 28 '21 at 12:41
  • But how can I use Discriminator in TPT approach? When I add a Discriminator in my Elements entity configuration then I get the following error: 'Pole' is mapped to the table 'Poles' while 'Element' is mapped to the table 'Elements'. Map all the entity types in the hierarchy to the same table or remove the discriminator and map all of them to different tables. – dtkturpal Mar 28 '21 at 12:56
  • I found out that it is not possible to use Discriminator in TPT approach, only with TPH. – dtkturpal Mar 28 '21 at 13:02