EF Code First Migrations creating extra foreign key

Eitan

I'm trying to create a Profile table to integrate with the Asp.Net Identity table: AspNetUsers. I'm using Code First Migrations with EF 6.0.

Here is my User class:

public class ApplicationUser : IdentityUser
    {
        public ApplicationUser()
        {

        }


        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public UserStatus Status { get; set; }
        public virtual Profile Profile { get; set; }
    }

Here is my Profile class:

public class Profile
    {
        public int ID { get; set; }
        public string ApplicationUserID { get; set; }
        public virtual ApplicationUser ApplicationUser { get; set; }
    }

I have the configuration in my DataContext:

modelBuilder.Entity<Profile>()
                .HasRequired<ApplicationUser>(m => m.ApplicationUser);

The weird thing is that when I run the migration it creates ApplicationUserID and ApplicationUser_Id in the database when all I want is to create ApplicationUserID and use that as the foreign key column. If I add the property AppliationUser_Id it'll add ApplicationUser_Id1. Anyone know how to fix this?

I want to make sure that the Foreign Key is on the profile table i.e. Profile has the column ApplicationUserID and not vice versa i.e. I don't want the AspNetUsers table to have a profile_ID column.

P.S. I'm using Fluent API, not data annotations.

octavioccl

If you want to configure an one to one relationship, Entity Framework requires that the primary key of the dependent must be used as the foreign key.

public class Profile
{
    public string ApplicationUserID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

Now, about the FK convention, due to that restriction, since there is no choice, Code First will just infer the FK for you.

Another thing is, the proper way to configure one-to-one relationship using Fluent Api is:

modelBuilder.Entity<Profile>()
            .HasRequired<ApplicationUser>(m => m.ApplicationUser)
            .WithOptional(a=>a.Profile);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Disable Foreign Key Constraint Code First EF

From Dev

EF Code First Foreign Key Same Table

From Dev

EF Code First foreign key with multiple keys

From Dev

EF Code First Foreign Key Relationship

From Dev

Code first approach not creating foreign key

From Java

EF Code First foreign key without navigation property

From Dev

EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint

From Dev

EF Code First Single foreign key to multiple parents

From Dev

EF Code First Fluent API specifying the Foreign Key property

From Dev

EF code first foreign key ignored, getting "Invalid column name"

From Dev

EF-Code First navigation property foreign key in complex type

From Dev

EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint

From Dev

Renaming foreign key properties in code first EF classes causes an exception

From Dev

Foreign key is created incorrrectly using EF code first

From Dev

DDD and EF Code-First Migrations

From Dev

EF 6 Code First Migrations vs dbinitializer

From Dev

EF Code First Migrations to Deploy Older Version

From Dev

Deploying offline with EF code-first migrations

From Dev

EF Code First Approach: Confused in EF Foreign Key constraint by fluent syntax

From Dev

EF Migrations: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

From Dev

Multiplicity is not valid in Role in relationship: EF code first one to one relationship with same primary key and foreign key

From Dev

EF Code First - mapping two foreign key columns in child table to the same primary key

From Dev

Circular foreign key code first

From Dev

EF6 Code First: Using Fluent API to declare a Foreign Key

From Dev

How to define a table that its primary key is constructed from 2 foreign keys with EF code-first

From Dev

Remove table name prefix in EF code first foreign key table column

From Dev

How to expose Foreign Key property to existing entity having navigational property using EF6 Code First

From Dev

EF5 Code First Automatic Migrations: Renaming Primary Key gives error: column names in each table must be unique

From Dev

Add-Migration creating empty migration in code migrations/code first

Related Related

  1. 1

    Disable Foreign Key Constraint Code First EF

  2. 2

    EF Code First Foreign Key Same Table

  3. 3

    EF Code First foreign key with multiple keys

  4. 4

    EF Code First Foreign Key Relationship

  5. 5

    Code first approach not creating foreign key

  6. 6

    EF Code First foreign key without navigation property

  7. 7

    EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint

  8. 8

    EF Code First Single foreign key to multiple parents

  9. 9

    EF Code First Fluent API specifying the Foreign Key property

  10. 10

    EF code first foreign key ignored, getting "Invalid column name"

  11. 11

    EF-Code First navigation property foreign key in complex type

  12. 12

    EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint

  13. 13

    Renaming foreign key properties in code first EF classes causes an exception

  14. 14

    Foreign key is created incorrrectly using EF code first

  15. 15

    DDD and EF Code-First Migrations

  16. 16

    EF 6 Code First Migrations vs dbinitializer

  17. 17

    EF Code First Migrations to Deploy Older Version

  18. 18

    Deploying offline with EF code-first migrations

  19. 19

    EF Code First Approach: Confused in EF Foreign Key constraint by fluent syntax

  20. 20

    EF Migrations: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

  21. 21

    Multiplicity is not valid in Role in relationship: EF code first one to one relationship with same primary key and foreign key

  22. 22

    EF Code First - mapping two foreign key columns in child table to the same primary key

  23. 23

    Circular foreign key code first

  24. 24

    EF6 Code First: Using Fluent API to declare a Foreign Key

  25. 25

    How to define a table that its primary key is constructed from 2 foreign keys with EF code-first

  26. 26

    Remove table name prefix in EF code first foreign key table column

  27. 27

    How to expose Foreign Key property to existing entity having navigational property using EF6 Code First

  28. 28

    EF5 Code First Automatic Migrations: Renaming Primary Key gives error: column names in each table must be unique

  29. 29

    Add-Migration creating empty migration in code migrations/code first

HotTag

Archive