We have following table structure for our problem domain –
-
Questionnaire – QuestionnaireID (Primary Key), QuestionID
-
Question = QuestionID, Description and other 12 propeties.
-
QuestionGroup = QuestionGroupID, Description and 5 other properties.
The QuestionID of Questionnaire table is related to both Question and QuestionGroup Table.
Now with entity framework i have common structure
-
Questionnaire – QuestionnaireID (Primary Key), QuestionID, QuestionDetails (Navigation Property of type QuestionBase).
-
QuestionBase (Parent class for Question and QuestioGroup)
-
Question
-
QuestionGroup
How i can map them together so QuestionDetails Property of Questionnaire will contain value from either Question or QuestionGroup usin Entity Framework Code-First.
There is no option of changing database as it is already exists, using EF new version is not problem.
Is it is possible to do so or not?
Thanks
If you cannot change the database you will not be able to map a
QuestionDetailsnavigation property and relatedQuestionIdforeign key property at all because you cannot have single navigation property targeting two related tables. It will work only ifQuestionBaseentity has related table as well and if you map it as TPT inheritance. Moreover primary key column in bothQuestionandQuestionGrouptables will have to have the same name.I doubt that you have referential constraint on your
QuestionIDinQuestionnairetable (satisfying relation with bothQuestionandQuestionGroup). That is the first red flag which should tell you that you will not be able to map it.