EF 6如何设置两个外键到同一个表 [英] EF 6 how to set two foreign keys to same table

查看:104
本文介绍了EF 6如何设置两个外键到同一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格UserForms有一个国家表的两个外键,但创建我的控制器和创建视图(对于UserForms模型),链接到外键的两个字段不会显示。我该如何解决这个问题?以下是两个模型:

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

public string FullNames {get;组; }
public Countries PersonalsCountry {get;组; }
public Countries BusinessCountry {get;组;


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

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

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


解决方案

格拉策是正确的。您应该在依赖实体上公开外键属性:

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

public string FullNames {get;组; }

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

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

这里我使用了 int ,但是如果这些导航属性中的任一个是可选的,那么您只需替换 int? System.Nullable< int> 替代(它将在数据库中创建一个 int NULL 列,而不是一个 int NOT NULL )。 p>

虽然EF不需要您公开导航属性,但通常是一个很好的做法。相信我。它将帮助您避免以后出现意外异常。事实上,一些EF异常消息实际上建议在实体类上显示外键属性,以帮助EF更好地了解如何映射关系。这是一个这样的例外的例子。注意附加信息部分:


{INSERT语句与FOREIGN KEY约束冲突
FK_dbo.DependentTable_dbo.PrincipalTable_Id 冲突
发生在数据库DatabaseName中,表dbo.PrincipalTable,列
'Id'。该语句已被终止。}



附加信息:保存
不会为其关系暴露外键属性的实体时发生错误。
EntityEntries属性将返回null,因为单个实体不能将
标识为异常的来源。通过在
中显示外键属性来实现保存,可以更容易地处理异常
。有关详细信息,请参阅InnerException。



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. 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. 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? or System.Nullable<int> instead (which will create an int NULL column in the database rather than an int NOT NULL).

Although EF does not require you to expose navigation properties, it is generally a good practice to. 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. 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."}

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. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.

这篇关于EF 6如何设置两个外键到同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆