4

I have a TPH (Table per Hierarchy) set up in Entity Framework, where I use a column called Discriminator to seperate the different EntityTypes.

I.e. when the Discriminator is 0 the type is Dog, if it's 1 it's Cat etc

Now I want to move some code for (mass)-updating from EF to a custom ExecuteSqlCommand.

Question: Is it possible to extract the Discriminator value from the Type through the MetadataWorkspace or by some other way?

I'm look for something like GetDiscriminatorValue<Cat>("ColumnName")

If not possible: is there another way so I don't have to hardcode those values somewhere in my code?

Thanks,

Dirk Boer
  • 8,522
  • 13
  • 63
  • 111
  • Possible Dupe http://stackoverflow.com/questions/4525953/can-i-access-the-discriminator-value-in-tph-mapping-with-entity-framework-4-ctp5 – Paul Nov 27 '16 at 22:01

1 Answers1

3

I haven't tried yet but it seems that you can get the value (and other non-public properties) using reflection and MetadataWorkSpace. Take a look at this example: http://blog.cincura.net/231942-tph-mapping-discriminator-condition-from-metadataworkspace/

Rob
  • 11,492
  • 14
  • 59
  • 94
  • 1
    There is an assumption somewhere that the condition is always a string (while mine was an int), but for the rest it seems to work! :) Thanks! – Dirk Boer Jun 12 '13 at 10:54