diff --git a/src/blog/Controllers/AdminController.cs b/src/blog/Controllers/AdminController.cs index 378f333..31b61ff 100644 --- a/src/blog/Controllers/AdminController.cs +++ b/src/blog/Controllers/AdminController.cs @@ -95,7 +95,7 @@ public async Task EditFeature(FeatureViewModel model) public async Task ManageFeatures() { AdminManageFeaturesViewModel amfvm = new AdminManageFeaturesViewModel(); - amfvm.Features = await _context.Features.ToListAsync(); + amfvm.Features = await _context.Features.AsNoTracking().ToListAsync(); return View(amfvm); } @@ -159,7 +159,7 @@ public async Task EditCategory(CategoryViewModel model) public async Task ManageCategories() { AdminManageCategoriesViewModel amcvm = new AdminManageCategoriesViewModel(); - amcvm.Categories = await _context.Categories.ToListAsync(); + amcvm.Categories = await _context.Categories.AsNoTracking().ToListAsync(); return View(amcvm); } @@ -176,13 +176,13 @@ public async Task DeleteCategory(long id) public async Task ManagePosts() { AdminManagePostsViewModel mpvm = new AdminManagePostsViewModel(); - mpvm.posts = await _context.BlogPosts.Include(a => a.Author).ToListAsync(); + mpvm.posts = await _context.BlogPosts.AsNoTracking().Include(a => a.Author).ToListAsync(); return View(mpvm); } public async Task ManageUsers() { - return View(await _userManager.Users.Include(u => u.Logins).Include(u => u.Author).ToListAsync()); + return View(await _userManager.Users.AsNoTracking().Include(u => u.Logins).Include(u => u.Author).ToListAsync()); } public ViewResult CreateUser() => View(); @@ -270,7 +270,7 @@ public async Task DeleteUser(string id) public IActionResult EditUser(string id) { - ApplicationUser user = _userManager.Users.Where(u => u.Id == id).Include(u => u.Author).Include(u => u.Logins).FirstOrDefault(); + ApplicationUser user = _userManager.Users.AsNoTracking().Where(u => u.Id == id).Include(u => u.Author).Include(u => u.Logins).FirstOrDefault(); if (user != null) { diff --git a/src/blog/Controllers/HomeController.cs b/src/blog/Controllers/HomeController.cs index ec3c832..d1c0f22 100644 --- a/src/blog/Controllers/HomeController.cs +++ b/src/blog/Controllers/HomeController.cs @@ -25,6 +25,7 @@ public HomeController(BlogDbContext context) public async Task Index() { var recentPosts = await _context.BlogPosts + .AsNoTracking() .Where(bp => bp.Public == true && bp.PublishOn < DateTime.Now) .OrderByDescending(bp => bp.ModifiedAt) .ThenByDescending(bp => bp.PublishOn) @@ -49,6 +50,7 @@ public async Task Search(string searchText) var search = "\"*" + searchText + "*\""; var posts = await _context.BlogPosts + .AsNoTracking() .FromSql("SELECT * from [dbo].[BlogPost] WHERE Contains((Content, Description, Title), {0})", search) .Where(bp => bp.Public == true && bp.PublishOn < DateTime.Now) .OrderByDescending(bp => bp.ModifiedAt) @@ -97,6 +99,7 @@ public async Task CategoryPosts(long? id) if (category != null) { var bpcs = await _context.BlogPostCategory + .AsNoTracking() .Where(bp => bp.CategoryId == category.Id && bp.BlogPost.Public == true && bp.BlogPost.PublishOn < DateTime.Now) .Include(bpc => bpc.BlogPost) .ThenInclude(bp => bp.Author) @@ -116,13 +119,14 @@ public async Task AllPosts(int? sortby = 1) // Sort by Category if (sortby == 1) { - var categories = await _context.Categories.ToListAsync(); + var categories = await _context.Categories.AsNoTracking().ToListAsync(); var viewCategories = new List(); foreach (var c in categories) { var bpcs = await _context.BlogPostCategory + .AsNoTracking() .Where(bp => bp.CategoryId == c.Id && bp.BlogPost.Public == true && bp.BlogPost.PublishOn < DateTime.Now) .Include(bpc => bpc.BlogPost) .ThenInclude(bp => bp.Author) @@ -151,6 +155,7 @@ public async Task AllPosts(int? sortby = 1) vm = new ViewAllPostsViewModel() { PostsByDate = await _context.BlogPosts + .AsNoTracking() .Where(bp => bp.Public == true && bp.PublishOn < DateTime.Now) .Include(bp => bp.Author) .OrderByDescending(bp => bp.ModifiedAt) @@ -173,6 +178,7 @@ public async Task FeaturePosts(long id) } var bpfs = await _context.BlogPostFeature + .AsNoTracking() .Where(bpf => bpf.FeatureId == feature.Id && bpf.BlogPost.Public == true && bpf.BlogPost.PublishOn < DateTime.Now) .Include(bpf => bpf.BlogPost) .ThenInclude(bp => bp.Author) @@ -216,7 +222,7 @@ public async Task Features() // cant select new into a defined type so have to use anon type for the select new here due to EF Core bug // code after is a work-around - var x = from feature in _context.Features + var x = from feature in _context.Features.AsNoTracking() select new { Title = feature.Title, @@ -224,7 +230,7 @@ public async Task Features() Id = feature.Id, BlogCount = (from posts in _context.BlogPostFeature where posts.FeatureId == feature.Id && posts.BlogPost.Public == true && posts.BlogPost.PublishOn < DateTime.Now - select posts).Count() + select posts).AsNoTracking().Count() }; List featureList = new List(); @@ -257,6 +263,7 @@ public async Task BlogPost(long? id) return NotFound(); var post = await _context.BlogPosts + .AsNoTracking() .Include(bp => bp.Author) .Include(bp => bp.BlogPostCategory) .ThenInclude(bpc => bpc.Category) diff --git a/src/blog/Views/Author/ManagePosts.cshtml b/src/blog/Views/Author/ManagePosts.cshtml index 5e9cea8..f8399a4 100644 --- a/src/blog/Views/Author/ManagePosts.cshtml +++ b/src/blog/Views/Author/ManagePosts.cshtml @@ -1,7 +1,7 @@ @* Author Manage Posts Page!!!*@ @{ - @using blog.Models.ViewModels.Author - @model AuthorManagePostsViewModel + @using blog.Models.ViewModels.Author + @model AuthorManagePostsViewModel ViewData["Title"] = "Manage Posts"; Layout = "_Layout"; } @@ -10,39 +10,43 @@
-

Manage Posts


- - - - - - - - - - - - - @foreach (var post in Model.posts) - { - var aid = $"a{post.Id}"; - - - - - - - - - } - -
IdTitleDescriptionPublish DateIs Public?
@post.Id@post.Title@post.Description@post.PublishOn@post.Public - - View - Edit - -
- Create New Post +
+

Manage Posts

+
+ + + + + + + + + + + + + @foreach (var post in Model.posts) + { + var aid = $"a{post.Id}"; + + + + + + + + + } + +
IdTitleDescriptionPublish DateIs Public?
@post.Id@post.Title@post.Description@post.PublishOn@post.Public + + View + Edit + +
+ Create New Post +
+
diff --git a/src/blog/Views/Shared/_Layout.cshtml b/src/blog/Views/Shared/_Layout.cshtml index ec9fbad..e280d49 100644 --- a/src/blog/Views/Shared/_Layout.cshtml +++ b/src/blog/Views/Shared/_Layout.cshtml @@ -27,7 +27,7 @@ -