1

I need to discriminate types on the following condition:

0 - type A
>0 - type B

Is it achievable using EF6/7 ?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • I think your question is not clear. Do you mean that you want to fill the Discriminator column with those 0 / >0 values instead of the name of the type? Or that you want EF to treat your entities as Type A / Type B based on a value of a property in the entity instance? I don't think the second thing is possible. – Diana Nov 19 '16 at 22:43
  • @Diana yes, the latter variant. – Pavel Voronin Nov 19 '16 at 23:23
  • I believe EF uses the CLR types of the entities to do its internal stuff, and there is no way to change that behaviour. EF needs the entities to be instances of the specific type, the mappings are based on those types. You could try to do a cast in your code, to convert the entities to the right type according to your condition. So I think what you need can not be done with EF, but perhaps I am wrong. – Diana Nov 20 '16 at 00:05
  • @Diana.In principle there is no huge difference between equality expression and any other boolean expression based on entity's properties. Except one thing. It's easy to check against intersections: Either descriminator column is 0, and ef need to materialize entity of type A, or it is 1, and then it is type B. – Pavel Voronin Nov 20 '16 at 07:32
  • You are right, but the point is, where would you put that code? Discriminator columns are not in your model, EF doesn't allow you to define a property for them. Also, if you try to map several different entity types to use the same value in their discriminator column you get run-time errors. Unless you modify the internal code of EF that instantiates/materializes the entities I don't know how you would achieve what you need. But as I said, perhaps I am wrong and it can be done. – Diana Nov 20 '16 at 15:15
  • @Diana >EF doesn't allow you to define a property for them. I've completely forgotten this fact =) – Pavel Voronin Nov 20 '16 at 16:43
  • 1
    Yep. I found [this interesting discussion](https://github.com/aspnet/EntityFramework/issues/4650) related to discriminators. Also, it seems [this future feature in EF](https://github.com/aspnet/EntityFramework/issues/240) may provide a solution for your issue. – Diana Nov 20 '16 at 17:12

0 Answers0