public ActionResult Comment(FormCollection coll) { using (TransactionScope scope = new TransactionScope()) { comment cmt = new comment { comment_datetime = DateTime.Now, comment_text = coll["comment_text"], user_id = Int32.Parse(coll["user_id"]) }; db.comments.Add(cmt); db.SaveChanges(); story_comments phComment = new story_comments { comment = cmt, story_id = Int32.Parse(coll["story_id"]) }; db.story_comments.Add(phComment); db.SaveChanges(); ViewBag.User = db.users.Where(u => u.id == cmt.user_id).SingleOrDefault(); scope.Complete(); return View(cmt); } }
public ActionResult Comment(FormCollection coll) { comment cmt = null; try { //we need to add entries to comments and photo_comments table // so lets start transaction using (TransactionScope scope = new TransactionScope()) { //first add to comment //INSERT INTO comment (comment_datetime, comment_text, user_id) VALUES (?,?,?) cmt = new comment { comment_datetime = DateTime.Now, comment_text = coll["comment_text"], user_id = Int32.Parse(coll["user_id"]) }; db.comments.Add(cmt); db.SaveChanges(); //now take the reference of comment and add to photo_comment photo_comments phComment = new photo_comments { comment = cmt, photo_id = Int32.Parse(coll["photo_id"]) }; db.photo_comments.Add(phComment); db.SaveChanges(); ViewBag.User = db.users.Include(m => m.membership).Where(u => u.id == cmt.user_id).SingleOrDefault(); scope.Complete(); return View(cmt); } } catch { return View(cmt); } }