简体   繁体   English

使用数据注释创建外键

[英]Create Foreign Key using Data Annotations

In the code below, I need to set a foreign key constrant on ParentInfoAddProperties.ParentQuestionAnswersId so that it is dependent on ParentQuestionAnswers.Id (which is a Primary Key). 在下面的代码中,我需要在ParentInfoAddProperties.ParentQuestionAnswersId上设置一个外键constrant,以便它依赖于ParentQuestionAnswers.Id(这是一个主键)。 I am attempting to do so using data annotations but Entity Framework 6 wants to create a new Foreign Key column in my ParentQuestionAnswers table which references the ParentInfoAddProperties.Id column not the ParentInfoAddProperties.ParentQuestionAnswersId column. 我试图使用数据注释,但实体框架6想要在我的ParentQuestionAnswers表中创建一个新的外键列,该列引用ParentInfoAddProperties.Id列而不是ParentInfoAddProperties.ParentQuestionAnswersId列。 I do not want Entity Framework to create a new foreign key column. 我不希望Entity Framework创建新的外键列。

I'd greatly appreciate if someone can explain what data annotations or (if necessary) fluent mappings I should specify to achieve the desired foreign key constrant. 如果有人能够解释我应该指定哪些数据注释或(如果需要)流畅的映射来实现所需的外键实例,我将不胜感激。 Thanks in advance. 提前致谢。

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; }
    }
}

You could use the following data annotation, and use entity instead of int 您可以使用以下数据注释,并使用实体而不是int

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

to get the Id only you could add the property 要获得Id,您只能添加该属性

public int ParentQuestionAnswersId { get; set; }

but you still need the ParentQuestionAnswers property so EF will understand you . 但你仍然需要ParentQuestionAnswers属性,所以EF会理解你。

(these code rows should be under the ParentInfoAddProperties class) (这些代码行应该在ParentInfoAddProperties类下)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用Entity Framework Code First数据注释创建双向外键关系吗? - Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations? 使用数据注释指定外键 - Specify Foreign Key with Data Annotations 实体框架 - 代码优先 - 数据注释 - 不必要的外键列 - Entity Framework - Code first - Data annotations - Unnecessary foreign key columns 如何在EF数据注释中指定外表键? - How to specify foreign table key in EF Data Annotations? 如何使用数据注释在实体框架中设置外键? - How to set up foreign key in Entity Framework with data annotations? 无法使用 efcore 创建可为空的外键 - Unable to create nullable foreign key using efcore 关键和必需数据注释 - Key and Required data annotations 如何在带有数据注释的实体框架中检索外键关系中的列子集 - How to retrieve a subset of columns in a foreign-key relationship in Entity Framework with Data Annotations 如何使用实体框架在FluentAPI /数据注释中定义外键可选关系? - How do I define Foreign Key Optional Relationships in FluentAPI/Data Annotations with the Entity Framework? 关于使用带有外键的linq保存数据 - About saving data using linq with foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM