Skip to content

Instantly share code, notes, and snippets.

@jstadnicki
Created February 11, 2024 13:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jstadnicki/9a97619c26e4ba9571fa2aec40be25d3 to your computer and use it in GitHub Desktop.
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication1.Models;
namespace WebApplication1.Controllers;
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly BlogDbContext _blogDbContext;
public HomeController(ILogger<HomeController> logger, BlogDbContext blogDbContext)
{
_logger = logger;
_blogDbContext = blogDbContext;
_blogDbContext.Database.EnsureCreated();
}
public IActionResult Index()
{
var movies = _blogDbContext.Posts.AsNoTracking().ToList();
return View(movies);
}
[HttpPost]
[Route("/create")]
public IActionResult Create(string title)
{
var movie = new Post
{
Id = Guid.NewGuid(),
Title = title,
ReleaseDate = DateTime.Now
};
_blogDbContext.Posts.Add(movie);
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit")]
public IActionResult Edit(Guid id, string title)
{
var m = _blogDbContext.Posts.Single(x => x.Id == id);
m.UpdateDate = DateTime.Now;
m.Title = title;
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit/update")]
public IActionResult EditUpdate(Guid id, string title)
{
var m = new Post
{
Id = id,
Title = title,
UpdateDate = DateTime.Now
};
_blogDbContext.Update(m); // This will mark the entity as modified
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit/attach")]
public IActionResult EditAttach(Guid id, string title)
{
var m = new Post
{
Id = id,
};
_blogDbContext.Posts.Attach(m);
m.Title = title;
m.UpdateDate = DateTime.Now;
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit/attach-v2")]
public IActionResult EditAttachV2(Guid id, string title)
{
var m = new Post
{
Id = id,
Title = $"{title}-v2",
UpdateDate = DateTime.Now
};
_blogDbContext.Posts.Attach(m);
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit/notrack-edit")]
public IActionResult NoTrackEdit(Guid id, string title)
{
var m = _blogDbContext.Posts.AsNoTracking().Single(x => x.Id == id);
m.UpdateDate = DateTime.Now;
m.Title = title;
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit/notrack-update")]
public IActionResult NoTrackUpdate(Guid id, string title)
{
var m = _blogDbContext.Posts.AsNoTracking().Single(x => x.Id == id);
m.UpdateDate = DateTime.Now;
m.Title = title;
_blogDbContext.Update(m); // This will mark the entity as modified
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit/notrack-attach")]
public IActionResult NoTrackAttach(Guid id, string title)
{
var m = _blogDbContext.Posts.AsNoTracking().Single(x => x.Id == id);
m.UpdateDate = DateTime.Now;
m.Title = title;
_blogDbContext.Posts.Attach(m);
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit/notrack-attach-v2")]
public IActionResult NoTrackAttachV2(Guid id, string title)
{
var m = _blogDbContext.Posts.AsNoTracking().Single(x => x.Id == id);
_blogDbContext.Posts.Attach(m);
m.UpdateDate = DateTime.Now;
m.Title = $"{title}-v2";
_blogDbContext.Entry(m).State = EntityState.Modified;
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/edit/notrack-attach-update")]
public IActionResult NoTrackAttachUpdate(Guid id, string title)
{
var m = _blogDbContext.Posts.AsNoTracking().Single(x => x.Id == id);
m.UpdateDate = DateTime.Now;
m.Title = title;
_blogDbContext.Posts.Attach(m);
_blogDbContext.Posts.Update(m);
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[Route("/delete")]
public IActionResult NoTrackAttachUpdate(Guid id)
{
var m = _blogDbContext.Posts.Single(x => x.Id == id);
_blogDbContext.Posts.Remove(m);
_blogDbContext.SaveChanges();
return RedirectToAction("Index");
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
public class BlogDbContext : DbContext
{
public BlogDbContext(DbContextOptions<BlogDbContext> options) : base(options)
{
}
public override int SaveChanges()
{
return base.SaveChanges();
}
public DbSet<Post> Posts { get; set; }
}
public class Post
{
public Guid Id { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public DateTime? UpdateDate { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment