【发布时间】:2014-07-22 22:43:54
【问题描述】:
在下面的代码中,我需要在 ParentInfoAddProperties.ParentQuestionAnswersId 上设置一个外键常量,以便它依赖于 ParentQuestionAnswers.Id(这是一个主键)。我正在尝试使用数据注释来执行此操作,但实体框架 6 想要在我的 ParentQuestionAnswers 表中创建一个新的外键列,该列引用 ParentInfoAddProperties.Id 列而不是 ParentInfoAddProperties.ParentQuestionAnswersId 列。我不希望 Entity Framework 创建新的外键列。
如果有人能解释我应该指定哪些数据注释或(如有必要)流畅映射来实现所需的外键常量,我将不胜感激。提前致谢。
namespace Project.Domain.Entities
{
public class ParentQuestionAnswers
{
public ParentQuestionAnswers()
{
ParentInfoAddProperties = new ParentInfoAddProperties();
}
[Required]
public int Id { get; set; }
[Required]
public int UserId { get; set; }
public ParentInfoAddProperties ParentInfoAddProperties { get; set; }
}
public class ParentInfoAddProperties
{
[Required]
public int Id { get; set; }
[Required]
public int ParentQuestionAnswersId { get; set; }
}
}
【问题讨论】:
标签: c# ef-code-first foreign-keys data-annotations entity-framework-6