0

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.

Sean Gough
  • 1,721
  • 3
  • 26
  • 47

1 Answers1

0

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670