public static void InsertOnSubmit(this DbSet<Comment> source, Comment comment) { if (comment.CommentID == default(int)) { // New entity source.Add(comment); } else { // Existing entity source.Attach(comment); } }
public ActionResult CreateComment(int articleId, string name, string email, string body) { TheBeerHouseDataContext dc = new TheBeerHouseDataContext(); Article article = dc.Articles.GetArticle(articleId); // throw a 404 Not Found if the requested article is not in the database if (article == null) throw new HttpException(404, "The article could not be found."); Comment comment = new Comment { AddedBy = name, AddedByEmail = email, AddedByIP = Request.UserHostAddress, AddedDate = DateTime.Now, Body = body }; article.Comments.Add(comment); // save changes to database dc.SubmitChanges(); return View(new { commentId = comment.CommentID, name = comment.AddedBy, body = comment.Body }); }
public static void DeleteOnSubmit(this DbSet<Comment> source, Comment comment) { source.Remove(comment); }