Entity Framework 7 Foreign Key Column Name

c# entity-framework entity-framework-core


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)

enter image description here

Is there any reason for this?

2/19/2016 12:04:12 PM

Accepted Answer

public int SofwareSystemId { get; set; }

read that property again. there's a t missing :)

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

2/19/2016 12:09:09 PM

Popular Answer

Use the ForeignKey and Column attributes to fix this:

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.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow