Saturday, 28 September 2013

How to sub-class with E.F. Database First

How to sub-class with E.F. Database First

Historically I have used database first in my applications. In cases where
I have a base class and multiple derived classes I end up having a lot of
'if' statements in my code.
I have just now started using Code First and I have noticed that the key
to E.F. mapping back to the derived classes is with the 'Discriminator'
field in the base class Sql table.
My question is, with regards to some of the existing applications I have
that use database first, how can I begin to create my POCO class in true
OO fashion? Am I able to add this Discriminator field to the database
table and the E.F. will create my subclasses for me? I guess this wouldn't
work as that is not enough information for E.F. to know which fields
belong to which class.
So perhaps the question becomes, is there any way to implement
polymorphism with database first or do I have to switch over to code
first. If this is the case, is it possible to turn db first into code
first and not lose any data?
Thanks.

No comments:

Post a Comment