简体   繁体   English

EF 6如何将两个外键设置为同一个表

[英]EF 6 how to set two foreign keys to same table

I have a table UserForms that has two foreign keys to a Countries table, but on creating my controller and create view (for the UserForms model) the two fields linking to the foreign keys do not appear. 我有一个表UserForms,它有一个States表的两个外键,但在创建我的控制器和创建视图(对于UserForms模型)时,不会出现链接到外键的两个字段。 What should I do to sort this problem? 我应该怎么做才能解决这个问题? Below are the two models: 以下是两种型号:

public class UserForms
{
     public int Id { get; set; }

     public string FullNames { get; set; }
     public Countries IndividualsCountry { get; set; }
     public Countries BusinessCountry { get; set; }
}

public class Countries
{
     public Countries()
     {
         this.STRBusinessCountry = new HashSet<UserForms>();
         this.STRIndividualsCountry = new HashSet<UserForms>();
     }

     public int Id { get; set; }
     public string NameOfCountry { get; set; }

     [InverseProperty("IndividualsCountry")]
     public virtual ICollection<UserForm> STRIndividualsCountry { get; set; }
     [InverseProperty("BusinessCountry")]
     public virtual ICollection<UserForm> STRBusinessCountry { get; set; }
 }

The comment left by @T.Glatzer is correct. @ T.Glatzer留下的评论是正确的。 You should expose foreign key properties on your dependent entities: 您应该在依赖实体上公开外键属性:

public class UserForms
{
    public int Id { get; set; }

    public string FullNames { get; set; }

    public int IndividualsCountryId { get; set; }
    [ForeignKey("IndividualsCountryId")]
    public virtual Countries IndividualsCountry { get; set; }

    public int BusinessCountryId { get; set; }
    [ForeignKey("BusinessCountryId")]
    public virtual Countries BusinessCountry { get; set; }
}

Here I used int , but if either of these navigation properties are optional, you would just substitute int? 这里我使用了int ,但如果这些导航属性中的任何一个是可选的,你只需要替换int? or System.Nullable<int> instead (which will create an int NULL column in the database rather than an int NOT NULL ). 或者System.Nullable<int> (它将在数据库中创建一个int NULL列而不是int NOT NULL )。

Although EF does not require you to expose navigation properties, it is generally a good practice to. 虽然EF不要求您公开导航属性,但通常是一个好习惯。 Trust me. 相信我。 It will help you avoid unexpected exceptions later on. 它可以帮助您以后避免意外的异常。 In fact, some EF exception messages actually recommend exposing foreign key properties on the entity classes to help EF better figure out how to map relationships. 事实上,一些EF异常消息实际上建议在实体类上公开外键属性,以帮助EF更好地弄清楚如何映射关系。 Here is an example of one such exception. 以下是一个此类例外的示例。 Note "Additional Information" section: 注意“附加信息”部分:

{"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.DependentTable_dbo.PrincipalTable_Id". The conflict occurred in database "DatabaseName", table "dbo.PrincipalTable", column 'Id'. The statement has been terminated."} {“INSERT语句与FOREIGN KEY约束冲突”FK_dbo.DependentTable_dbo.PrincipalTable_Id“。冲突发生在数据库”DatabaseName“,表”dbo.PrincipalTable“,列'Id'。语句已被终止。”}

Additional information: An error occurred while saving entities that do not expose foreign key properties for their relationships. 附加信息:保存不公开其关系的外键属性的实体时发生错误。 The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. EntityEntries属性将返回null,因为无法将单个实体标识为异常源。 Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. 通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常。 See the InnerException for details. 有关详细信息,请参阅InnerException。

@danludwig thanks for expounding @T.Glatzer answer this has worked for me! @danludwig感谢你解释@ T.Glatzer回答这对我有用! thank you. 谢谢。 my final code that is now working is 我现在正在使用的最终代码是

public class UserForms
    {
        public int Id { get; set; }

        public string FullNames { get; set; }
    [ForeignKey("IndividualsCountry")]
        public int? IndividualsCountryId { get; set; }
    [ForeignKey("BusinessCountry")]
        public int? BusinessCountryId { get; set; }

        public virtual Countries IndividualsCountry { get; set; }
    public virtual Countries BusinessCountry { get; set; }
    }

public class Countries
    {
        public Countries()
        {
            this.STRBusinessCountry = new HashSet<UserForms>();
            this.STRIndividualsCountry = new HashSet<UserForms>();
        }

        public int Id { get; set; }
        public string NameOfCountry { get; set; }

        [InverseProperty("IndividualsCountry")]
        public virtual ICollection<UserForms> STRIndividualsCountry { get; set; }
        [InverseProperty("BusinessCountry")]
        public virtual ICollection<UserForms> STRBusinessCountry { get; set; }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM