Create Foreign Key using Data Annotations

c# data-annotations ef-code-first entity-framework-6 foreign-keys

Accepted Answer

The following data annotation could be utilized, but entity should be used instead of int.

[Required]
[ForeignKey("ParentQuestionAnswers")]
public ParentQuestionAnswers ParentQuestionAnswers { get; set; }

You could add the property to obtain the Id alone.

public int ParentQuestionAnswersId { get; set; }

nevertheless you still require theParentQuestionAnswers property for EF to comprehend you.

(These code rows need to appear beneath theParentInfoAddProperties class)

6
6/2/2014 7:00:04 PM



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow