简体   繁体   English

在表中使用两个外键指向同一个表的惊人命名

[英]Surprising naming in table with two foreign keys to the same table

I have the following entity (which was created using database-first). 我有以下实体(它是使用数据库优先创建的)。 It has two foreign keys to the AspNetUser table. 它具有两个AspNetUser表的外键。

public partial class Message
{
    public int Id { get; set; }
    public string ToUserId { get; set; }
    public string FromUserId { get; set; }
    public string Message1 { get; set; }
    public bool Viewed { get; set; }
    public System.DateTime TimeStampUtc { get; set; }

    public virtual AspNetUser AspNetUser { get; set; }
    public virtual AspNetUser AspNetUser1 { get; set; }
}

I find that AspNetUser corresponds to FromUserId , and AspNetUser1 corresponds to ToUserId . 我发现AspNetUser对应于FromUserId ,而AspNetUser1对应于ToUserId This surprises me since ToUserId is defined first in the table, I'd expect the 1 to be appended to the one that comes second. 这使我感到惊讶,因为ToUserId在表中首先定义,我希望将1附加到第二个表中。

I can code for it either way, but I can't code for it if the rules are somewhat random as to how this is determined. 我可以用任何一种方式为它编写代码,但是如果规则是如何确定的,那么我就不能编写代码。 It's critical that I understand the rules and know which one will be associated with which foreign key. 了解规则并知道哪个规则将与哪个外键相关至关重要。

Does anyone know the rules involved here? 有人知道这里涉及的规则吗?

In your edmx you can rename the navigation properties by clicking on the property and changing the Name . 在edmx中,您可以通过单击属性并更改Name来重命名导航属性。 This will survive EDMX updates. 这将在EDMX更新中保留下来。 However, if you totally delete it from EDMX and then build it from the database again, then in that case you will need to rename it again. 但是,如果您完全将其从EDMX中删除,然后再次从数据库中进行构建,那么在这种情况下,您将需要重新命名。 Otherwise, you should be fine. 否则,你应该没事。

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

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