diff --git a/Teknik/Areas/Paste/PasteHelper.cs b/Teknik/Areas/Paste/PasteHelper.cs index 367ffeb..2e350f3 100644 --- a/Teknik/Areas/Paste/PasteHelper.cs +++ b/Teknik/Areas/Paste/PasteHelper.cs @@ -136,7 +136,7 @@ namespace Teknik.Areas.Paste using (TeknikEntities db = new TeknikEntities(optionsBuilder.Options)) { - var paste = GetPaste(db, cache, url); + var paste = GetPaste(db, cache, url, false); if (paste != null) { paste.Views++; @@ -149,9 +149,15 @@ namespace Teknik.Areas.Paste public static Models.Paste GetPaste(TeknikEntities db, ObjectCache cache, string url) { - var paste = cache.AddOrGetObject(url, (key) => db.Pastes.FirstOrDefault(up => up.Url == key)); + return GetPaste(db, cache, url, true); + } - if (paste != null && + public static Models.Paste GetPaste(TeknikEntities db, ObjectCache cache, string url, bool attach) + { + var paste = cache.AddOrGetObject(url, (key) => db.Pastes.AsNoTracking().FirstOrDefault(up => up.Url == key)); + + if (attach && + paste != null && !db.Exists(paste)) db.Attach(paste); diff --git a/Teknik/Areas/Upload/Controllers/UploadController.cs b/Teknik/Areas/Upload/Controllers/UploadController.cs index 7cdefe3..73a837e 100644 --- a/Teknik/Areas/Upload/Controllers/UploadController.cs +++ b/Teknik/Areas/Upload/Controllers/UploadController.cs @@ -228,7 +228,7 @@ namespace Teknik.Areas.Upload.Controllers long contentLength = 0; DateTime dateUploaded = new DateTime(); - var upload = UploadHelper.GetUpload(_dbContext, _cache, file); + var upload = UploadHelper.GetUpload(_dbContext, _cache, file, false); if (upload != null) { // Check Expiration @@ -416,7 +416,7 @@ namespace Teknik.Areas.Upload.Controllers { if (_config.UploadConfig.DownloadEnabled) { - Models.Upload upload = UploadHelper.GetUpload(_dbContext, _cache, file); + Models.Upload upload = UploadHelper.GetUpload(_dbContext, _cache, file, false); if (upload != null) { // Check Expiration @@ -460,7 +460,7 @@ namespace Teknik.Areas.Upload.Controllers public IActionResult DeleteByKey(string file, string key) { ViewBag.Title = "File Delete | " + file ; - Models.Upload upload = UploadHelper.GetUpload(_dbContext, _cache, file); + Models.Upload upload = UploadHelper.GetUpload(_dbContext, _cache, file, false); if (upload != null) { DeleteViewModel model = new DeleteViewModel(); diff --git a/Teknik/Areas/Upload/UploadHelper.cs b/Teknik/Areas/Upload/UploadHelper.cs index 8ddd6c3..60e4cd6 100644 --- a/Teknik/Areas/Upload/UploadHelper.cs +++ b/Teknik/Areas/Upload/UploadHelper.cs @@ -160,7 +160,7 @@ namespace Teknik.Areas.Upload using (TeknikEntities db = new TeknikEntities(optionsBuilder.Options)) { - var upload = GetUpload(db, cache, url); + var upload = GetUpload(db, cache, url, false); if (upload != null) { upload.Downloads++; @@ -173,9 +173,15 @@ namespace Teknik.Areas.Upload public static Models.Upload GetUpload(TeknikEntities db, ObjectCache cache, string url) { - var upload = cache.AddOrGetObject(url, (key) => db.Uploads.FirstOrDefault(up => up.Url == key)); + return GetUpload(db, cache, url, true); + } - if (upload != null && + public static Models.Upload GetUpload(TeknikEntities db, ObjectCache cache, string url, bool attach) + { + var upload = cache.AddOrGetObject(url, (key) => db.Uploads.AsNoTracking().FirstOrDefault(up => up.Url == key)); + + if (attach && + upload != null && !db.Exists(upload)) db.Attach(upload);