0

I am new to entity framework and right now struggling with an issue. I am using MVC api and EF6.

I was saving member details in a PUT method. That worked fine.

Now, I modified the code to save a comment (Added new method PublishComment()) as well but this breaks the code without any error message!! The debug session just hungs on the db.SaveChanges().

public void Put(Guid id, MemberListItem item)
    {
using (Context db = new Context())
        {
            Person updPerson = db.People.Find(item.PersonID);

            if (updPerson.PrincipleContact != item.PrincipalMember)
            {                    
                updPerson.PrincipleContact = item.PrincipalMember;
            }
            string memberName = updPerson.GivenName1;
            Guid memberID = updPerson.MemberID;                

            db.Entry(updPerson).State = System.Data.Entity.EntityState.Modified;
            PublishComment(db, memberID, "User Modified. " + memberName + " modified from user profile.");
            db.SaveChanges();
        }


public void PublishComment(Context db, Guid memberID, string comment)
    {
        MemberComment newComment = new MemberComment();
        newComment.CommentID = new Guid();
        newComment.MemberID = memberID;
        newComment.DateAdded = DateTime.Now;
        newComment.Comment = DateTime.Now.ToShortDateString() +": " + comment;
        db.MemberComments.Add(newComment);            

    }
Jay
  • 133
  • 2
  • 12
  • I don't see anything wrong with the code. Can you try to run (not debug) and see whether it is still breaking. Sometime Visual studio needs a restart ! – Shyju Nov 18 '15 at 01:28
  • Is CommentID not a `uniqueidentifier` in the database with an autogenerated value? Is there a foreign key between `Person` and `MemberComment`? Also, I don't see where you're actually using the `id` of the `Put` method. – Mike Guthrie Nov 18 '15 at 02:28

1 Answers1

1

I think your issue is here:

newComment.CommentID = new Guid(); // eg {00000000-0000-0000-0000-000000000000}

But I'm not too sure why you are not seeing a duplicate key exception (which is what this looks like it would generate)

see Guid is all 0's (zeros)?

Community
  • 1
  • 1
undefined
  • 33,537
  • 22
  • 129
  • 198
  • Yup you are right. Thanks Luke. I wonder too why EF didn't throw any exception. – Jay Nov 18 '15 at 02:43