I'm creating a POCO model to use with entity framework code first CTP5. I'm using the <key()> decoration to make a property map to a PK column. But how can I define a PK on more then one column, and specifically, how can I control order of the columns in the index? Is it a result of the order of properties in the class?
Edit: look at @kara's answer for an updated solution.
Thanks!
In EF or EF Core, the Column Order attribute is not supported for specifying key order, so the best practice is to configure composite keys in the OnModelCreating method of your DbContext.
public class MyEntity
{
public int MyFirstKeyProperty { get; set; }
public int MySecondKeyProperty { get; set; }
public string MyThirdKeyProperty { get; set; }
// Other properties
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.HasKey(e => new { e.MyFirstKeyProperty, e.MySecondKeyProperty, e.MyThirdKeyProperty });
base.OnModelCreating(modelBuilder);
}