I’m using ASP.NET MVC 3 with the Entity Framework 4 code first approach and every time I try to specify composite keys using the key attribute on my models, I get this error:
System.Data.Edm.EdmAssociationConstraint: : Number of Properties in
the Dependent and Principal Role in a relationship constraint must be
exactly identical.
I’m using the column attribute to differentiate ordering of the primary keys like so:
public class Game
{
[Key, Column(Order=0)]
public Guid GameId { get; set; }
[Key, Column(Order=1)]
public string Name { get; set; }
public string Description { get; set; }
public Game()
{
this.GameId = Guid.NewGuid();
}
}
I would like to know if there is another approach to creating composite keys, or perhaps there is a way to stop getting this error? I know that it’s possible to add logic to the OnModelBuild event, but I’d rather use the key attributes on the model if possible.
Try to exclude property Name from the entity key (that I would recommend). Or, use it in all entities if you really need to make it part of the key.