I try to model many to many relationship between Car
and Employee
where the info about when the car was used are held. So I need a class inbetween which will hold those additional attributes.
The SSCCE:
I have Person
class:
public class Person {
public int Id { get; set};
}
and a class Employee
which derives from Person
:
public class Employee : Person {
public virtual ICollection<EmployeeCar> EmployeeCar { get; set; }
}
and a association classEmployeeCar
which holds attributes dateFrom
, dateTo
:
public class EmployeeCar {
[Key, Column(Order = 0)]
public virtual Car Car { get; set; }
[Key, Column(Order = 1)]
public virtual Employee Employee { get; set; }
public DateTime DateFrom{ get; set; }
public DateTime DateTo { get; set; }
}
and finally class Car
:
public class Car {
public int Id { get; set; }
public virtual ICollection<EmployeeCar> EmployeeCar { get; set; }
}
in ApplicationDbContext
I hold:
public DbSet<Person> Persons { get; set; }
public DbSet<EmployeeCar> EmployeesCars { get; set; }
public DbSet<Car> Cars { get; set; }
but when I run the database-update
I get:
MyApp.EmployeeCar: : EntityType 'EmployeeCar' has no key defined. Define the key for this EntityType. EmployeesCars: EntityType: EntitySet 'EmployeesCars' is based on type 'EmployeeCar' that has no keys defined.
How to fix that situation? How to tell Entity Framework that combination of Car
and Employee
is the key?