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

1.2k Views Asked by At

ConsumerNM is a NM/bridge table/entity.

Its a 1:N relation to Events table.

When I do an insert I get the question title exception.

What do I have to change to make it work?

DO I have to create 2 foreign keys each one pointing to the other ConsumerNM_Key?

 public class ConsumerNM
 {
        public ConsumerNM()
        {
           Events = new HashSet<Event>();
        }

        [Key]
        [Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int FK_LEADMETA { get; set; }

        [Key]
        [Column(Order = 1)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int FK_LEADCONSUMER { get; set; }

        public virtual ICollection<Event> Events { get; set; }      
}

 public class Event
    {
        [Key]
        public int Id { get; set; }

        public DateTime EventDate { get; set; }       

        public virtual ConsumerNM Consumer { get; set; }

        [ForeignKey("Consumer")]
        public int FK_Consumer { get; set; }

    }
1

There are 1 best solutions below

4
Fabio On BEST ANSWER

ConsumerNM or Event are incorrect. You have two options:

First Option:

 public class ConsumerNM
 {
        //This is not a PK
        //[Key]
        //[Column(Order = 0)]
        //[DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int FK_LEADMETA { get; set; }

        [Key]
        public int FK_LEADCONSUMER { get; set; }  
}

Event remains unchanged.

Second Option:

public class Event
{
    [Key]
    public int Id { get; set; }

    public DateTime EventDate { get; set; }       

    public virtual ConsumerNM Consumer { get; set; }

    [ForeignKey("Consumer")]
    public int FK_LEADCONSUMER { get; set; }

    [ForeignKey("Consumer")]
    public int FK_LEADMETA { get; set; }

}

ConsumerNM remains unchanged.