Wednesday, 18 September 2013

Entity framework and using composite primary keys?

Entity framework and using composite primary keys?

I don't seem to be able to get the following relationship done. MyEvent
has composite primary key on UserId and LocationId. It also has a virtual
property that is based on these 2 primary keys.
public class MyEvent
{
public string UserId {get; set;}
public string LocationId {get; set;}
public virtual EventDescription EventDescription {get; set;}
}
public MyEventConfiguration()
{
HasKey(a => new { a.UserId, a.LocationId});
HasRequired(a => a.EventDescription).WithMany().HasForeignKey(a => new
{ a.UserId, a.LocationId }).WillCascadeOnDelete(false);
}
I get the following error:
One or more validation errors were detected during model generation:
System.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in
Role 'MyEvent_EventDescription_Source' in relationship
'MyEvent_EventDescription'. Because the Dependent Role refers to the key
properties, the upper bound of the multiplicity of the Dependent Role must
be '1'.

No comments:

Post a Comment