public ActionResult Create(PostModel model) { string returnUrl = HttpContext.Request.Params["returnUrl"].Length > 0 ? HttpContext.Request.Params["returnUrl"] : HttpContext.Request.UrlReferrer.ToString(); if(ModelState.IsValid) { try { Entities dbContext = new Entities(); Models.Object obj = new Models.Object(); obj.EntityName = "Post"; dbContext.Objects.AddObject(obj); dbContext.SaveChanges(); Post post = new Post(); post.Id = obj.Id; post.UserId = new Guid(UserHelper.getLoggedInUserId()); post.PersonId = new Guid(HttpContext.Request.Params["personId"].Length > 0 ? HttpContext.Request.Params["personId"] : UserHelper.getLoggedInUserId()); post.Content = model.Content; post.CreatedAt = DateTime.Now; post.UpdatedAt = DateTime.Now; dbContext.Posts.AddObject(post); dbContext.SaveChanges(); return Redirect(returnUrl); }catch(Exception ex) { ModelState.AddModelError("", ex.Message); } } return View(model); }
/// <summary> /// Deprecated Method for adding a new object to the Posts EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToPosts(Post post) { base.AddObject("Posts", post); }
/// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="userId">Initial value of the UserId property.</param> /// <param name="personId">Initial value of the PersonId property.</param> /// <param name="createdAt">Initial value of the CreatedAt property.</param> public static Post CreatePost(global::System.Int32 id, global::System.Guid userId, global::System.Guid personId, global::System.DateTime createdAt) { Post post = new Post(); post.Id = id; post.UserId = userId; post.PersonId = personId; post.CreatedAt = createdAt; return post; }