public ActionResult Categories(Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); } ViewBag.Categories = db.Categories.ToList(); return(View(category)); }
public async Task Can_get_post() { // Arrange var post = new Post { Id = Guid.NewGuid().ToString(), Title = "Climate", Description = "Global warming", PostedOn = DateTime.UtcNow, TrainedMeanRateValue = 2, Features = new double[] { 1, 0.5, 0.3, 5 }, Article = new Article { Content = "Global warming is the long-term heating of Earth’s climate system", Image = "data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P" } }; using var context = new PersonalBlogContext(_contextOptions); context.Add(post); context.SaveChanges(); var repository = new Repository <Post>(context); // Act var receivedPost = await repository.Get(p => p.Id == post.Id).FirstOrDefaultAsync(); // Assert receivedPost.Should().BeEquivalentTo(post, options => options .Excluding(p => p.Id) .Excluding(p => p.Article.Id)); }
public ActionResult Create(Post post) { try { db.Posts.Add(post); db.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult validationError in ex.EntityValidationErrors) { Response.Write("Object: " + validationError.Entry.Entity.ToString()); Response.Write(""); foreach (DbValidationError err in validationError.ValidationErrors) { Response.Write(err.ErrorMessage + ""); } } } return(RedirectToAction("PostInfo", new { id = post.Id })); }
public void FindRecord_WhenExists_ShouldWorks() { var options = new DbContextOptionsBuilder <PersonalBlogContext>() .UseInMemoryDatabase(databaseName: "Find") .Options; using (var context = new PersonalBlogContext(options)) { context.ArticleCategories.Add(new ArticleCategory { Id = 1, Name = "Name", Description = "Description" }); context.SaveChanges(); } using (var context = new PersonalBlogContext(options)) { var result = context.ArticleCategories.Find(1); Assert.IsNotNull(result); } }
public int SaveChanges() { return(_context.SaveChanges()); }
public ActionResult Create(User user) { db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("ListPosts", "Post")); }