Reputation: 1266
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
Upvotes: 0
Views: 377
Reputation: 364279
If you cannot change the database you will not be able to map a QuestionDetails
navigation property and related QuestionId
foreign key property at all because you cannot have single navigation property targeting two related tables. It will work only if QuestionBase
entity has related table as well and if you map it as TPT inheritance. Moreover primary key column in both Question
and QuestionGroup
tables will have to have the same name.
I doubt that you have referential constraint on your QuestionID
in Questionnaire
table (satisfying relation with both Question
and QuestionGroup
). That is the first red flag which should tell you that you will not be able to map it.
Upvotes: 1