简体   繁体   中英

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.

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 . 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.

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 . This will survive EDMX updates. 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. Otherwise, you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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