我正在使用EdmAssociationConstraint ::关系中的从属角色和主要角色中的属性数限制了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; }
}
答案 0 :(得分:7)
您的Trigger类具有复合键(TriggerId,TaskId)。
但是您的TriggerHistory具有触发器导航属性,ForeignKey
仅使用TaskId属性定义。
尝试在触发器属性上更改ForeignKey
,如下所示:
[ForeignKey("TriggerId,TaskId")]
public virtual Trigger Triggers { get; set; }
也许Triggers属性应命名为“Trigger”,因为这不是Trigger对象的集合。