daljit
daljit

Reputation: 1266

entity framework - one entity property linked to two table columns

We have following table structure for our problem domain -

  1. Questionnaire - QuestionnaireID (Primary Key), QuestionID

  2. Question = QuestionID, Description and other 12 propeties.

  3. 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

  1. Questionnaire - QuestionnaireID (Primary Key), QuestionID, QuestionDetails (Navigation Property of type QuestionBase).

  2. QuestionBase (Parent class for Question and QuestioGroup)

  3. Question

  4. 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

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

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

Related Questions