Multiple Inheritance with Entity Framework with TPH

*爱你&永不变心* 提交于 2019-12-09 07:56:27
SteveChapman

I know this question is ages old, but I spent a long time researching a similar problem in a recent adventure with EF 5.0 (and 6.0.0-beta1), and this article led me to a valid solution. Thanks to the author.

I have a complex hierarchy mapped using TPH:

  • A1 - B1 - C1
  • A1 - B1 - C2
  • A1 - B2 - C3
  • A1 - B2 - C4
  • A1 - B2 - C5
  • A1 - B2 - C6 - D1 - E1
  • A1 - B2 - C6 - D1 - E2
  • A1 - B2 - C6 - D1 - E3
  • A1 - B2 - C6 - D2 - E4
  • A1 - B2 - C6 - D2 - E5
  • A1 - B2 - C6 - D2 - E6
  • A1 - B2 - C6 - D3 - E7
  • A1 - B2 - C6 - D3 - E8
  • A1 - B2 - C6 - D3 - E9

Where bold indicates abstract, and italic indicates additional mappings specific to that class (and subclasses) to the same table (of course).

Basically the solution was to create 4 discriminator columns in the table, and map each level in the hierarchy to a different discriminator column. Note that the abstract classes in level B and below also need to be mapped and have a discriminator value specified.

A lot of time wasted on this, all the examples and documentation of TPH seem to only cover the basics. In the real world things can get a little more complex!

Hope this helps someone.

You seem to be trying to do table-per-hierarchy mapping (because I see only one table in your SSDL), but I don't see that you have set up a discriminator mapping in the CSDL. There should be a Condition node, like this:

            <Condition ColumnName="NotificationType" Value="Announcement"/>

In short, if you are trying to use a table per hierarchy mapping, you should review the instructions for configuring this and follow them, because you have not completed all of the steps.

If, on the other hand, you intend to do table per type mapping, then you seem to have missed a couple of tables when you updated from your database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!