Let's use a simple example:
public class Employee
{
public int EmployeeID { get; set; }
public ICollection<Pay> Pays { get; set; }
}
public class Pay
{
public Employee Employee { get; set; }
public int Year { get; set; }
public double Amount { get; set; }
}
Is there any way to use the fluent API to create a Pays table with a primary key on Employee_EmployeeID, Year
(using EF4.1 column conventions)?
I don't want to use data annotations but I tried this anyway:
public class Pay
{
[Key, Column(Order = 0)]
public Employee Employee { get; set; }
[Key, Column(Order = 1)]
public int Year { get; set; }
public double Amount { get; set; }
}
All that got me was a primary key on Year
开发者_开发问答and a foreign key on Employee_EmployeeID
though.
This will be only possible if you also add FK property to your Pay
:
public class Pay
{
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
public int Year { get; set; }
public double Amount { get; set; }
}
Now you can map it with fluent-api:
modelBuilder.Entity<Pay>()
.HasKey(p => new
{
p.EmployeeId,
p.Year
});
You need employee's FK as a property if you want to make it part of PK.
精彩评论