0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dosihris
  • 85
  • 1
  • 8
  • right now you have a two keys on applicationuser, and two one-column FK's from Country to ApplicationUser. I'm not sure how to do this in data annotation, in fluent API it is modelBuilder.Entity().HasKey(x=>new{x.Id, x.Version}); and modelBuilder.Entity().HasRequired(x=>x.Chief).WithMany().HasForeignKey(x=>new{x.Chief_Id, x.Chief_Version}); – DevilSuichiro Sep 14 '16 at 05:58

1 Answers1

0

Look into your foreign key nameof(Chief), you might not have the full relationship set up.

There is another question that might help here.

Community
  • 1
  • 1
Wurd
  • 465
  • 2
  • 15