私は次のモデルを持っています
public class User
{
public int UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string FacebookID { get; set; }
public string GoogleID { get; set; }
public string TwitterID { get; set; }
public bool Admin { get; set; }
public bool Authorized { get; set; }
public virtual Comment Comment { get; set; }
public virtual CommentReply CommentReply { get; set; }
public virtual Project Project { get; set; }
public virtual ProjectVote ProjectVote { get; set; }
public virtual CommentReplyVote CommentReplyVote { get; set; }
public virtual CommentVote CommentVote { get; set; }
public virtual ProjectCoverVote ProjectCoverVote { get; set; }
}
public class Comment
{
[Key]
public int CommentID { get; set; }
public int ProjectDocID { get; set; }
public int UserID { get; set; }
public string Text { get; set; }
public string Annotation { get; set; }
public string Quote { get; set; }
public string Start { get; set; }
public string End { get; set; }
public string StartOffset { get; set; }
public string EndOffset { get; set; }
public DateTime DateCreated { get; set; }
public virtual ICollection<CommentVote> CommentVote { get; set; }
public virtual ICollection<CommentReply> CommentReply { get; set; }
public virtual ProjectDoc ProjectDoc { get; set; }
public virtual User User { get; set; }
}
次に、これをエンティティコードの最初の移行のシードメソッドの一部として使用しました。
context.Users.AddOrUpdate(i => i.FirstName,
new User { UserID = 1, FirstName = "Bob", LastName = "Dole", Email = "nobody@gmail.com", FacebookID = "123123124123", Admin = true, Authorized = true },
new User { UserID = 2, FirstName = "Dale", LastName = "Dole", Email = "nobody@gmail.com", FacebookID = "123123124123", Admin = false, Authorized = true }
);
context.Comments.AddOrUpdate(i => i.CommentID,
new Comment { CommentID = 1, ProjectDocID = 1, UserID = 1, Text = "This is a comment", DateCreated = DateTime.Parse("Dec 03, 2011 12:00:00 PM") }
);
これは私が得るエラーです。
System.Data.SqlClient.SqlException:
The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_dbo.Comment_dbo.User_CommentID". The conflict occurred in database
"TEST_ba6946e96d174b7bac9543ba614c8cf8", table "dbo.User", column 'UserID'.
コメント行を追加しようとしてコメントアウトすると、UserIDフィールドを持つProjectsテーブルで正常に機能します。ヘルプ?関係のないエラーや私が知っておくべきことを見つけた場合は、遠慮なく指摘してください。