public IActionResult SavePost(IFormCollection form) { if (!IsLoggedIn()) { return(Redirect("/admin")); } // retrieve user input from the form int? postID = Convert.ToInt32(form["hdnPostId"]); int userID = Convert.ToInt32(form["hdnUserId"]); string title = form["txtTitle"]; string url = form["txtUrl"]; string summary = form["txtSummary"]; string content = form["txtContent"]; string search = form["txtSearch"]; DateTime dateCreated = form["hdnDateCreated"].ToString().ToDateTime(); DateTime dateModified = DateTime.Now; bool published = (form["chkPublished"] == "on"); bool pageable = (form["chkPageable"] == "on"); int loggedInUserID = HttpContext.Session.GetInt32(_loggedInUserId) ?? 0; // create Post object var post = new SlickCMS.Data.Entities.Post { PostId = postID ?? 0, UserId = userID != 0 ? userID : loggedInUserID, Title = title, Content = content, DateCreated = dateCreated, DateModified = dateModified, Pageable = pageable ? 1 : 0, Published = published ? 1 : 0, Search = search, Summary = summary, Url = url, }; // save post if (postID == null || postID == 0) { //_context.Post.Add(post); _context.Set <Data.Entities.Post>().Add(post); postID = post.PostId; } else { //_context.Post.Update(post); _context.Set <Data.Entities.Post>().Update(post); } _context.SaveChanges(); SaveCategories(postID, form); SaveTags(postID, form); return(Redirect("/admin/posts")); }
/// <summary> /// Specifies whether to use EntityState for CRUD operations /// </summary> /*private bool useEntityState * { * get { return true; } * }*/ public virtual void Add(IBaseEntity entity) { /*using (var db = CreateContext()) * { * if (useEntityState) * { * db.Entry(entity).State = System.Data.EntityState.Added; * } * else * { * db.Set<TEntity>().Add(entity); * } * * db.SaveChanges(); * }*/ _context.Add <IBaseEntity>(entity); _context.SaveChanges(); }