Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint

I am having EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint this exception in EF 5

I want to fix this issue using data annotation

public class Task
{
    [Key, Column(Order = 0)]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int TaskId { get; set;}
    public string Name { get; set; }
}

public class Trigger
{
    [Key, Column(Order = 0)]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int TriggerId { get; set; }
    public string Name { get; set; }

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

    [ForeignKey("TaskId")]
    public virtual Task Tasks { get; set; }
}

public class TriggerHistory
{
    [Key, Column(Order = 0)]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int TriggerHistoryId { get; set; }

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

    [ForeignKey("TaskId")]
    public virtual Task Task { get; set; }

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

    [ForeignKey("TaskId")]
    public virtual Trigger Triggers { get; set; }

    public string Name { get; set; }
}
like image 353
Naveen Avatar asked Jun 28 '13 16:06

Naveen


1 Answers

Your Trigger class has composit key (TriggerId, TaskId). But your TriggerHistory has Trigger navigation property with ForeignKey defined using only TaskId property.

Try to change ForeignKey on Triggers property like this:

[ForeignKey("TriggerId,TaskId")]
public virtual Trigger Triggers { get; set; }

And maybe the Triggers property should be named "Trigger" as this is not the collection of Trigger objects.

like image 88
Jurica Smircic Avatar answered Nov 15 '22 06:11

Jurica Smircic