具有INSERT语句的表在完全不同的表中发生冲突

时间:2014-01-01 19:16:58

标签: entity-framework

我收到以下错误。

  

INSERT语句与FOREIGN KEY约束“FK_dbo.NotificationObject_dbo.Comment_CommentReplyID”冲突。冲突发生在数据库“DB_f0f7b19b55c44199988352815f71b25c”,表“dbo.Comment”,列“CommentID”。

我很困惑,因为NotificationObject表可以有多种可选的外键,包括注释的键或注释的回复。当我尝试添加一个CommentReplyID的行时,它会给出上面的错误...关于注释表???键应该指向CommentReply表,所以我很困惑。

请参阅下面的相关模型。

public class NotificationObject
{
    public int ID { get; set; }
    public int NotificationTypeID { get; set; }
    public int? CommentID { get; set; }
    public int? CommentVoteID { get; set; }
    public int? CommentReplyID { get; set; }
    public int? CommentReplyVoteID { get; set; }
    public int? UserID { get; set; }
    public int? ActivityCommentID { get; set; }
    public DateTime DateCreated { get; set; }

    public virtual NotificationType NotificationType { get; set; }
    public virtual Comment Comment { get; set; }
    public virtual Comment CommentVote { get; set; }
    public virtual Comment CommentReply { get; set; }
    public virtual Comment CommentReplyVote { get; set; }
    public virtual ActivityComment ActivityComment { get; set; }
    public virtual ICollection<NotificationActor> NotificationActor { get; set; }
}

public class Comment
{
    public int CommentID { get; set; }
    public int ProjectDocID { get; set; }
    public int UserID { get; set; }
    public string text { get; set; }
    public string quote { get; set; }
    public DateTime DateCreated { get; set; }

    public virtual ICollection<CommentVote> CommentVote { get; set; }
    public virtual ICollection<CommentReply> CommentReply { get; set; }
    public virtual ICollection<CommentReport> CommentReport { get; set; }
    public virtual ProjectDoc ProjectDoc { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<ranges> ranges { get; set; }
}

为什么不让它为CommentReplyID表中的NotificationObject字段添加值?

1 个答案:

答案 0 :(得分:2)

CommentReply中的NotificationObject导航属性定位Comment类型,而不是CommentReply类型。