I solved the problem with the order of the foreign key columns in the database when using independent association using EF migration.
Turn off automatic migration and create an initial migration for your data model.
For model class:
public class Session { public int? Id { get; set; } public Track Track { get; set; } public Car Car { get; set; } public int Event { get; set; } public DateTime Date { get; set; } public string Name { get; set; } }
migration code will be generated:
CreateTable("dbo.Sessions", c => new { Id = c.Int(nullable: false, identity: true), Event = c.Int(nullable: false), Date = c.DateTime(nullable: false), Name = c.String(nullable: false, maxLength: 64), Car = c.Int(nullable: false), Track = c.Int(nullable: false), }) ...
Now just rearrange the columns of the foreign key (car, track) by moving them up. When you create a new database and open the table, the column order will be as expected.
source share