1

I have the following Administrator class and a AdministratorImage class with a foreign key relationship as shown in the classes.

public class Administrator
{
    // Pk
    public Guid AdministratorId { get; set; }

    //Properties
    public string Name { get; set; }
    public string MiddleName { get; set; }
    public string Surname { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }

    public virtual AdministratorImage AdministratorImage { get; set; }



}


public class AdministratorImage : ImageBase
{
    // PK
    public int AdministratorImageId { get; set; }

    // Fks
    public Guid AdministratorId { get; set; }
    public virtual Administrator Administrator { get; set; }
}

The challenge that i'm having is that when creating the application's database tables using migrations commands, I notice that an extra line of code is added automatically to mapp the Foreign Key relationships instead the code typed to define the relationship. Please see the create "administrator table" which shows the correct Administrator class Primary Key.

CreateTable(
            "dbo.Administrator",
            c => new
                {
                    AdministratorId = c.Guid(nullable: false),
                    Name = c.String(nullable: false, maxLength: 40),
                    MiddleName = c.String(maxLength: 40),
                    Surname = c.String(nullable: false, maxLength: 40),
                    UserName = c.String(nullable: false, maxLength: 40),
                    Email = c.String(nullable: false, maxLength: 100),

                })
            .PrimaryKey(t => t.AdministratorId);

Please see also the code for the create "AdministratorImage" table.

CreateTable(
            "dbo.AdministratorImage",
            c => new
                {
                    AdministratorImageId = c.Int(nullable: false, identity: true),
                    AdministratorId = c.Guid(nullable: false),
                    ImageName = c.String(),
                    FileSize = c.Int(nullable: false),
                    ImageData = c.Binary(),
                    ImageType = c.Int(nullable: false),
                    CurrentSelected = c.Boolean(nullable: false),
                    Administrator_AdministratorId = c.Guid(nullable: false),
                })
            .PrimaryKey(t => t.AdministratorImageId)
            .ForeignKey("dbo.Administrator", t => t.Administrator_AdministratorId)
            .Index(t => t.Administrator_AdministratorId);

This code show an extra line of code created automatically

Administrator_AdministratorId = c.Guid(nullable: false),

which defines the Foreign Key relationship

.ForeignKey("dbo.Administrator", t => t.Administrator_AdministratorId)

instead of using the default Foreign Key AdministratorId by convention. Is there a way to prevent this and to get Entity Framework to map the correct Foreign Key relationship when creating tables

Ntu
  • 41
  • 4
  • 1
    See for instance [One to One Relationship with Different Primary Key in EF 6.1 Code First](https://stackoverflow.com/questions/44550386/one-to-one-relationship-with-different-primary-key-in-ef-6-1-code-first/44551078#44551078) – Ivan Stoev Feb 03 '18 at 16:51
  • See this too http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx – Lucian Bumb Feb 03 '18 at 16:57

0 Answers0