简体   繁体   中英

Entity Framework 7 Foreign Key Column Name

I have a MasterUserApprovalOfficial entity with two foreign keys, MasterUserId and SoftwareSystemId. EF 7 is smart enough to figure out that these two properties are foreign keys.Here is my MasterUserApprovalOfficial class

public class MasterUserApprovalOfficial
{
    public int MasterUserApprovalOfficialId { get; set; }

    public int MasterUserId { get; set; }

    public MasterUser MasterUser { get; set; }

    public int SofwareSystemId { get; set; }

    public SoftwareSystem SoftwareSystem { get; set; }

    public bool Active { get; set; }

    public DateTime CreateDate { get; set; }

    public MasterUserApprovalOfficial()
    {
        CreateDate = DateTime.Now;
    }
}

If I look at the table that was created the MasterUserId column was created and named as expected, the SoftwareSystemId column however is created as SoftwareSystemSoftwareSystemId (The name of the class appended to the name of the primary key)

在此处输入图片说明

Is there any reason for this?

public int SofwareSystemId { get; set; }

read that property again. there's at missing :)

the second answer is also right, you don't need properties for the foreign key. A member of the class is enough

Use the ForeignKey and Column attributes to fix this:

[ForeignKey("SoftwareSystem"),Column("SoftwareSystemId")]
public int SoftwareSystemId { get; set; }

By default the foreign key will be added with "Id" behind it, but because you already have a property with that name (and EF doesn't recognize it's the foreignkey apparantly) it gets changed into SoftwareSystemSoftwareSystemId .

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