I have the following Classes and am using EF 4.2 Code First
public class PartAttribute
{
public Part Part { get; set; }
public PartAttributeType PartAttributeType { get; set; }
[Timestamp]
public byte[] Time { get; set; }
[Required]
public string Value { get; set; }
}
public class Part
{
public Guid Id { get; set; }
[Required]
public PartType PartType { get; set; }
[Required]
public string SerialNumber { get; set; }
public string Description { get; set; }
public ICollection<Team> SelectedTeams { get; set; }
}
public class PartAttributeType
{
public Guid Id { get; set; }
[Required]
public PartType PartType { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
}
When I build my model it creates the three tables and the relationships between the tables as I would like/expect them. I am trying to create a composite key on the PartAttribute table between the Part, PartAttributeType, and the time and I can’t seem to figure it out. When I try to add
modelBuilder.Entity<PartAttribute>().HasKey(c => new { c.Part, c.PartAttributeType, c.Time });
I get an error saying Part is not a scalar type (which it is not).
You need to introduce foreign key properties which can act as primary keys at the same time:
Then your mapping:
EF should be able to recognize the new properties as the foreign keys for your two navigation properties due to the naming convention.