1

I have a table "Project" in SQL database with field 'Discriminator'. The C# class 'Project' has two child classes, 'PropertyA' and 'PropertyB'. 'PropertyA' further has 2 child classes 'SaleA' and 'SaleB'.

The records for all the classes are being stored in one SQL table 'Project' with Discriminator values 'PropertyB', 'SaleA' and 'SaleB'.

'SaleA' has a field 'CNumber' and now I want to create a field 'CNumber' in 'PropertyB' class. And the values for 'CNumber' for both the above classes should be stored in the 'CNumber' column. I am using code first approach and I when I add a migration for the adding the new field, it adds a new column named 'PropertyB_CNumber' in DbContext.

How can I map the values from two different classes to one column?

Simran
  • 13
  • 3
  • I believe you will have to configure this manually. In the DbContext class. – AliK Oct 06 '21 at 04:56
  • What version of EF are you using? – Caius Jard Oct 06 '21 at 05:09
  • Also, this sounds wonky; if CNumber is applicable to ClassA and ClassB then would it not be more logical to have it in the parent of ClassA/B, rather than having it in both? – Caius Jard Oct 06 '21 at 05:12
  • @CaiusJard CNumber is not applicable to SaleB, so cannot add it to the parent – Simran Oct 11 '21 at 00:54
  • Mmmm.. It's a bit code smelly, but probably the easiest thing is going to be putting it on the parent and hiding it in B - https://stackoverflow.com/questions/1875401/how-to-hide-an-inherited-property-in-a-class-without-modifying-the-inherited-cla - i've never done it but I wonder if it's possible to tell EF that, in SaleB, that it's NotMapped – Caius Jard Oct 11 '21 at 08:38

0 Answers0