Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

Reft

Entity Framework is creating two unnecessary columns in my AccountCompanyRole table.

AccountCompany

public class AccountCompany
{
    [Key, Column(Order = 0), ForeignKey("Account")]
    public int AccountID { get; set; }

    [Key, Column(Order = 1), ForeignKey("Company")]
    public int CompanyID { get; set; }

    public virtual Account Account { get; set; }

    public virtual Company Company { get; set; }

    public virtual ICollection<AccountCompanyRole> AccountCompanyRoles { get; set; }
}

AccountCompanyRoles

public class AccountCompanyRole
{
    [Key, Column(Order = 0), ForeignKey("AccountCompany")]
    public int AccountID { get; set; }

    [Key, Column(Order = 1), ForeignKey("AccountCompany")]
    public int CompanyID { get; set; }

    [Key, Column(Order = 2), ForeignKey("Role")]
    public int RoleID { get; set; }

    public virtual AccountCompany AccountCompany { get; set; }

    public virtual Role Role { get; set; }
}

OnModelCreating

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AccountCompanyRole>()
            .HasRequired(p => p.AccountCompany)
            .WithMany()
            .WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);
    }

Result in DB

Result in DB

As you can see, EF has added two extra foreign keys in AccountCompanyRoles even tho I specified primary key and foreign key in Data Annotations.

Questions

  1. How do I prevent it from creating AccountCompany_AccountID and AccountCompany_CompanyID?

  2. Is this the best structure for what I am after? Should I instead use one primarykey and put IsUnique index on AccountID and CompanyID? This would probably solve my problem..

Indregaard

You have to specify the foreign keys in your fluent relationship like this:

modelBuilder.Entity<AccountCompanyRole>()
            .HasRequired(p => p.AccountCompany)
            .WithMany().HasForeignKey(d => new { d.AccountID, d.CompanyID })
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<AccountCompany>()
            .HasMany(c => c.AccountCompanyRoles)
            .WithRequired(d => d.AccountCompany)
            .HasForeignKey(e => new {e.AccountID, e.CompanyID});

Edit: Added fluent config for AccountCompany

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations?

From Dev

Exchanged Foreign Key on Entity Framework Code First From data base

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Foreign Key in Code First Entity Framework

From Dev

Entity Framework: Foreign Key in code first

From Dev

Defining multiple Foreign Key for the Same table in Entity Framework Code First

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Entity Framework 6 Code First Foreign Key Without Corresponding Properties

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Get foreign key value using Entity Framework code first

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Entity framework code first, get access to foreign key value

From Dev

Entity framework code first, custom foreign key name for inheritance

From Dev

How to specify a foreign key with code-first Entity Framework

From Dev

Entity Framework Indexing ALL foreign key columns

From Dev

Code First entity framework and foreign keys

From Dev

Entity Framework Code First initializing foreign keys

From Dev

Code first foreign key association MVC Entity

From Dev

Include foreign key in composite primary key in Code-First Entity Framework

From Dev

How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

From Dev

Entity Framework 6 multiple table to one foreign key relationship code first

From Dev

Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

From Dev

Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

From Dev

Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

From Dev

Entity framework 6 code first saving collection via foreign key relation

Related Related

  1. 1

    Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations?

  2. 2

    Exchanged Foreign Key on Entity Framework Code First From data base

  3. 3

    Entity Framework Code First Foreign Key issue

  4. 4

    Entity Framework Code First Foreign Key issue

  5. 5

    Foreign Key in Code First Entity Framework

  6. 6

    Entity Framework: Foreign Key in code first

  7. 7

    Defining multiple Foreign Key for the Same table in Entity Framework Code First

  8. 8

    Entity Framework Code First Foreign Key adding Index as well

  9. 9

    Entity Framework 6 Code First Foreign Key Without Corresponding Properties

  10. 10

    Entity Framework Code First and Firebird - Foreign Key name issue

  11. 11

    Entity Framework Code first adds unwanted foreign key column

  12. 12

    Multiple Foreign Key for Same table in Entity Framework Code First

  13. 13

    Get foreign key value using Entity Framework code first

  14. 14

    Entity Framework one to optional foreign key code first fluent mapping

  15. 15

    Entity framework code first cant create primary and foreign key relationship

  16. 16

    Entity framework code first, get access to foreign key value

  17. 17

    Entity framework code first, custom foreign key name for inheritance

  18. 18

    How to specify a foreign key with code-first Entity Framework

  19. 19

    Entity Framework Indexing ALL foreign key columns

  20. 20

    Code First entity framework and foreign keys

  21. 21

    Entity Framework Code First initializing foreign keys

  22. 22

    Code first foreign key association MVC Entity

  23. 23

    Include foreign key in composite primary key in Code-First Entity Framework

  24. 24

    How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

  25. 25

    Entity Framework 6 multiple table to one foreign key relationship code first

  26. 26

    Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

  27. 27

    Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

  28. 28

    Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

  29. 29

    Entity framework 6 code first saving collection via foreign key relation

HotTag

Archive