0
votes

Database build error:

One or more validation errors were detected during model generation:

Key_Authorities_Source_Key_Authorities_Target: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

Key Class:


    [Table("Keys")]
    public class Key
    {
        [Key, Column(Order = 0)]
        public int Id { get; set; }

        [Key, Column(Order = 1)]
        public int OwnedByFId { get; set; }

        [Key, Column(Order = 2)]
        public int OwnedByUId { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        [ForeignKey("Id"), Column(Order = 1)]
        public virtual ICollection Authorities { get; set; }
    }

Key Authorities Class:


    [Table("Key_Auths")]
    public class KeyAuthorities
    {
        [Key, Column(Order = 0)]
        public int Id { get; set; }

        [Key, Column(Order = 1)]
        public int KeyId { get; set; }

        public int DoorId { get; set; }

        public int VehicleId { get; set; }

        public int GateId { get; set; }
    }

Problem:

I already read several other Stack Overflow Questions regarding this problem and tried a bunch of stuff but I still can't figure out why this doesn't let me set this foreign keys.

I would really appreciate some help :c

2

2 Answers

1
votes

The error message actually is telling you that the number of properties don't match. This is because a your Key class is uniquely identified by 3 properties (the 3 PK that you define: Id, OwnedByFId and OwnedByUId), but your trying to define a Foreign Key to your Key Class using only the Id.

You must set all the PKs on your foreign class:

[Table("Key_Auths")]
public class KeyAuthorities
{
    [Key, Column(Order = 0)]
    public int Id { get; set; }

    [Key, Column(Order = 1)]
    [ForeignKey("Id, OwnedByFId, OwnedByUId")]
    public int KeyId { get; set; }

    public int DoorId { get; set; }
    
    public int VehicleId { get; set; }

    public int GateId { get; set; }
}

Notice that I added the data annotation [ForeignKey("Id, OwnedByFId, OwnedByUId")].

0
votes
// error message is basically telling that you have 
// not configured the keys and their order properly in "Keys" table

public class Keys {
    [Key, Column(Order = 0)]
    public int Key_1 { get; set; }

    [Key, Column(Order = 1)]
    public int Key_2 { get; set; }

    // order is important here as defined in "KeyAuthorities" table
    [ForeignKey("KeyAuthorities", Column(Order = 0)]
    public int KeyAuthorities_Key_1 { get; set; }

    [ForeignKey("KeyAuthorities", Column(Order = 1)]
    public int KeyAuthorities_Key_2 { get; set; }

    public virtual ICollection KeyAuthorities { get; set; }
}

public class KeyAuthorities {
    [Key, Column(Order = 0)]
    public int KeyAuthorities_Key_1 { get; set; }

    [Key, Column(Order = 1)]
    public int KeyAuthorities_Key_2 { get; set; }

}