5

我有 EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint this exception in EF 5

我想使用数据注释来解决这个问题

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; }
}
4

1 回答 1

7

您的 Trigger 类具有复合键(TriggerId、TaskId)。但是您的 TriggerHistory 具有ForeignKey仅使用 TaskId 属性定义的触发导航属性。

尝试ForeignKey像这样更改 Triggers 属性:

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

也许 Triggers 属性应该命名为“Trigger”,因为这不是 Trigger 对象的集合。

于 2013-06-28T17:13:27.547 回答