I'm working with Entity Framework and identity framework (IdentityUser
, IdentityRole
). I have a table with a composite key (table Country
) that points to the Users
table.
Unfortunately, EF can only build a relation when all keys are the same, otherwise you'll get this:
The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
So, how can I handle this? Right now I have tried to add this composite key to the ApplicationUser:IdentityUser
too, but in this way I have to add the composite key to all entities of the identity framework (that means user, role, claim, login, ...).
Here are my model classes:
class Country
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column(Order = 1)]
public int ID { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column(Order = 2)]
public int Version { get; set; }
[Required]
[ForeignKey(nameof(Chief))]
[Column(Order = 1)]
public string Chief_Id { get; set; }
[Required]
[ForeignKey(nameof(Chief)), Column(Order = 2)]
public int Chief_Version { get; set; }
public virtual ApplicationUser Chief{ get; set; }
}
class ApplicationUser : IdentityUser
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column(Order = 1)]
public override string Id
{
get { return base.Id; }
set { base.Id = value; }
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column(Order = 2)]
public int Version { get; set; }
}
Nico