From 4e7a725fee596b4b2a81817d3be80f4299e561a9 Mon Sep 17 00:00:00 2001
From: Glax <johanna@bernloehr.eu>
Date: Sun, 18 May 2025 13:53:23 +0200
Subject: [PATCH] Load entry references and collections

---
 API/Schema/Jobs/DownloadAvailableChaptersJob.cs     | 1 +
 API/Schema/Jobs/MoveMangaLibraryJob.cs              | 2 ++
 API/Schema/Jobs/RetrieveChaptersJob.cs              | 4 +++-
 API/Schema/Jobs/UpdateChaptersDownloadedJob.cs      | 1 +
 API/Schema/Jobs/UpdateSingleChapterDownloadedJob.cs | 2 ++
 5 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/API/Schema/Jobs/DownloadAvailableChaptersJob.cs b/API/Schema/Jobs/DownloadAvailableChaptersJob.cs
index e4d2eed..c6cfd8f 100644
--- a/API/Schema/Jobs/DownloadAvailableChaptersJob.cs
+++ b/API/Schema/Jobs/DownloadAvailableChaptersJob.cs
@@ -36,6 +36,7 @@ public class DownloadAvailableChaptersJob : Job
     
     protected override IEnumerable<Job> RunInternal(PgsqlContext context)
     {
+        context.Entry(Manga).Reference<LocalLibrary>(m => m.Library).Load();
         return Manga.Chapters.Where(c => c.Downloaded == false).Select(chapter => new DownloadSingleChapterJob(chapter, this));
     }
 }
\ No newline at end of file
diff --git a/API/Schema/Jobs/MoveMangaLibraryJob.cs b/API/Schema/Jobs/MoveMangaLibraryJob.cs
index 365ee1f..18f5437 100644
--- a/API/Schema/Jobs/MoveMangaLibraryJob.cs
+++ b/API/Schema/Jobs/MoveMangaLibraryJob.cs
@@ -42,6 +42,8 @@ public class MoveMangaLibraryJob : Job
     
     protected override IEnumerable<Job> RunInternal(PgsqlContext context)
     {
+        context.Entry(Manga).Collection<Chapter>(m => m.Chapters).Load();
+        context.Entry(Manga).Reference<LocalLibrary>(m => m.Library).Load();
         Dictionary<Chapter, string> oldPath = Manga.Chapters.ToDictionary(c => c, c => c.FullArchiveFilePath);
         Manga.Library = ToLibrary;
         try
diff --git a/API/Schema/Jobs/RetrieveChaptersJob.cs b/API/Schema/Jobs/RetrieveChaptersJob.cs
index 49f6db9..5d943a4 100644
--- a/API/Schema/Jobs/RetrieveChaptersJob.cs
+++ b/API/Schema/Jobs/RetrieveChaptersJob.cs
@@ -40,6 +40,7 @@ public class RetrieveChaptersJob : Job
     
     protected override IEnumerable<Job> RunInternal(PgsqlContext context)
     {
+        context.Entry(Manga).Collection<Chapter>(m => m.Chapters).Load();
         // This gets all chapters that are not downloaded
         Chapter[] allChapters = Manga.MangaConnector.GetChapters(Manga, Language).DistinctBy(c => c.ChapterId).ToArray();
         Chapter[] newChapters = allChapters.Where(chapter => Manga.Chapters.Contains(chapter) == false).ToArray();
@@ -47,7 +48,8 @@ public class RetrieveChaptersJob : Job
 
         try
         {
-            context.Chapters.AddRange(newChapters);
+            foreach (Chapter newChapter in newChapters)
+                Manga.Chapters.Add(newChapter);
             context.SaveChanges();
         }
         catch (DbUpdateException e)
diff --git a/API/Schema/Jobs/UpdateChaptersDownloadedJob.cs b/API/Schema/Jobs/UpdateChaptersDownloadedJob.cs
index 41228ea..b05d874 100644
--- a/API/Schema/Jobs/UpdateChaptersDownloadedJob.cs
+++ b/API/Schema/Jobs/UpdateChaptersDownloadedJob.cs
@@ -36,6 +36,7 @@ public class UpdateChaptersDownloadedJob : Job
     
     protected override IEnumerable<Job> RunInternal(PgsqlContext context)
     {
+        context.Entry(Manga).Collection<Chapter>(m => m.Chapters).Load();
         return Manga.Chapters.Select(c => new UpdateSingleChapterDownloadedJob(c, this));
     }
 }
\ No newline at end of file
diff --git a/API/Schema/Jobs/UpdateSingleChapterDownloadedJob.cs b/API/Schema/Jobs/UpdateSingleChapterDownloadedJob.cs
index 8ff69bf..55bcec4 100644
--- a/API/Schema/Jobs/UpdateSingleChapterDownloadedJob.cs
+++ b/API/Schema/Jobs/UpdateSingleChapterDownloadedJob.cs
@@ -37,6 +37,8 @@ public class UpdateSingleChapterDownloadedJob : Job
 
     protected override IEnumerable<Job> RunInternal(PgsqlContext context)
     {
+        context.Entry(Chapter).Reference<Manga>(c => c.ParentManga).Load();
+        context.Entry(Chapter.ParentManga).Reference<LocalLibrary>(m => m.Library).Load();
         Chapter.Downloaded = Chapter.CheckDownloaded();
 
         try