1

I have the following models

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

Then I used this as part of the seed method in entity code first migrations:

 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") }
      );

This is the error I get.

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'.

If I comment out the attempt to add the comment row it works fine for my Projects table that has a UserID field. Help? If you spot any unrelated errors or things I should be aware please don't hesitate to point them out.

Jed Grant
  • 1,305
  • 1
  • 17
  • 47
  • Could it be that `User` has an identity coulumn `UserId`, so the values in the script are ignored? (And the user with Id = 1 has been deleted) – Gert Arnold Dec 14 '12 at 00:05
  • Do you insert `UserID = 1` in `Project`s as well? – Gert Arnold Dec 14 '12 at 00:19
  • Gert, yes, there's a project row with a UserID = 1. – Jed Grant Dec 14 '12 at 00:29
  • OK, that's really weird. Maybe you should try to set the User object, rather than the key value. – Gert Arnold Dec 14 '12 at 00:31
  • Not sure if I totally understand. I tried this, assuming it's what you meant: Replaced "UserID = 1" with "User = new User { UserID = 1 }" but that just creates a new row in the Users tables and a "0" in the comment row. – Jed Grant Dec 14 '12 at 00:35
  • No, I mean: make a variable of the first user (`var u = new User { UserID = 1, ...` and in Comment: `User = u`. – Gert Arnold Dec 14 '12 at 00:36

1 Answers1

0

This appears to have been a result of a series of issues from needing to use the fluent api to tell it not to cascade on delete to setting the virtual attributes on the User class to be an ICollection<>. I was also using things like

 public virtual ProjectDoc ProjectDoc { get; set; }

Instead of

 public ProjectDoc ProjectDoc { get; set; }

Not sure what the difference is exactly, but it's working now.

Jed Grant
  • 1,305
  • 1
  • 17
  • 47