@@ -19,7 +19,7 @@ namespace DazContentInstaller.ViewModels;
1919
2020public class MainWindowViewModel : ViewModelBase
2121{
22- private readonly IDbContextFactory < ApplicationDbContext > _dbContextFactory = null ! ;
22+ private readonly ApplicationDbContext _dbContext = null ! ;
2323 private readonly SettingsService _settingsService = null ! ;
2424 public ObservableCollection < LoadedArchive > LoadedArchives { get ; set ; } = [ ] ;
2525 private InstalledArchiveTree InstalledArchivesTree { get ; } = [ ] ;
@@ -140,10 +140,10 @@ public MainWindowViewModel()
140140 RemoveLoadedArchiveClick = ReactiveCommand . Create < LoadedArchive > ( RemoveLoadedArchive ) ;
141141 }
142142
143- public MainWindowViewModel ( IDbContextFactory < ApplicationDbContext > dbContextFactory ,
143+ public MainWindowViewModel ( ApplicationDbContext dbContext ,
144144 SettingsService settingsService ) : this ( )
145145 {
146- _dbContextFactory = dbContextFactory ;
146+ _dbContext = dbContext ;
147147 _settingsService = settingsService ;
148148 }
149149
@@ -162,11 +162,10 @@ private void ClearLoadedArchives()
162162
163163 public async Task LoadAssetLibrariesAsync ( )
164164 {
165- await using var dbContext = await _dbContextFactory . CreateDbContextAsync ( ) ;
166165 AssetLibraries . Clear ( ) ;
167166 await Task . Run ( async ( ) =>
168167 {
169- await foreach ( var library in dbContext . AssetLibraries . AsAsyncEnumerable ( ) )
168+ await foreach ( var library in _dbContext . AssetLibraries . AsAsyncEnumerable ( ) )
170169 AssetLibraries . Add ( library ) ;
171170 } ) ;
172171
@@ -181,15 +180,14 @@ public async Task LoadInstalledArchivesAsync()
181180
182181 await Task . Run ( async ( ) =>
183182 {
184- await using var dbContext = await _dbContextFactory . CreateDbContextAsync ( ) ;
185- var archivesQuery = dbContext . Archives
183+ var archivesQuery = _dbContext . Archives
186184 . Where ( d => d . Status == ArchiveStatus . Installed ) ;
187185 if ( CurrentSelectedAssetLibrary is not null )
188186 archivesQuery = archivesQuery . Where ( d => d . AssetLibraryId == CurrentSelectedAssetLibrary . Id ) ;
189187
190188 await foreach ( var archive in archivesQuery . OrderBy ( d => d . ArchiveName . ToLower ( ) ) . AsAsyncEnumerable ( ) )
191189 {
192- var files = await dbContext . AssetFiles . Where ( d => d . ArchiveId == archive . Id ) . ToListAsync ( ) ;
190+ var files = await _dbContext . AssetFiles . Where ( d => d . ArchiveId == archive . Id ) . ToListAsync ( ) ;
193191 archive . AssetFiles = files ;
194192
195193 var node = InstalledArchivesTree . LoadArchive ( archive ) ;
@@ -220,10 +218,8 @@ private async Task InstallArchives()
220218
221219 await Task . Run ( async ( ) =>
222220 {
223- await using var dbContext = await _dbContextFactory . CreateDbContextAsync ( ) ;
224-
225221 var archivesToInstallNames = archivesToInstall . Select ( GetLoadedArchiveName ) . ToArray ( ) ;
226- var existingArchives = await dbContext . Archives
222+ var existingArchives = await _dbContext . Archives
227223 . Where ( a => a . AssetLibraryId == CurrentSelectedAssetLibrary . Id &&
228224 archivesToInstallNames . Contains ( a . ArchiveName ) )
229225 . Include ( a => a . AssetFiles )
@@ -253,8 +249,8 @@ await Task.Run(async () =>
253249 } ;
254250
255251 dbArchive . AssetFiles . AddRange ( archive . ContainedFiles ) ;
256- dbContext . Archives . Add ( dbArchive ) ;
257- await dbContext . SaveChangesAsync ( ) ;
252+ _dbContext . Archives . Add ( dbArchive ) ;
253+ await _dbContext . SaveChangesAsync ( ) ;
258254
259255 Dispatcher . UIThread . Post ( ( ) => LoadedArchives . Remove ( archive ) ) ;
260256 }
@@ -282,10 +278,9 @@ private async Task UninstallArchiveAsync()
282278 return ;
283279
284280 AllowArchiveLoad = false ;
285- await using var dbContext = await _dbContextFactory . CreateDbContextAsync ( ) ;
286281
287282 var selectedInstallArchiveIds = SelectedInstallNodes . Select ( n => n . DbId ) . ToArray ( ) ;
288- var archives = await dbContext . Archives
283+ var archives = await _dbContext . Archives
289284 . Include ( a => a . AssetLibrary )
290285 . Include ( a => a . AssetFiles )
291286 . Where ( a => selectedInstallArchiveIds . Contains ( a . Id ) )
@@ -304,7 +299,7 @@ await Task.Run(async () =>
304299 messageProgress . Report ( $ "Reading { selectedInstallArchiveIds . Length } archives to uninstall...") ;
305300
306301 var archiveIds = archives . Select ( a => a . Id ) . ToArray ( ) ;
307- var deleteFileExceptions = await dbContext . AssetFiles
302+ var deleteFileExceptions = await _dbContext . AssetFiles
308303 . Where ( f => ! archiveIds . Contains ( f . ArchiveId ) && f . InstalledPath != null )
309304 . ToListAsync ( ) ;
310305
@@ -323,8 +318,8 @@ await Task.Run(async () =>
323318 var uninstaller = new DazArchiveUninstaller ( archive ) ;
324319 await uninstaller . UninstallArchiveAsync ( deleteFileExceptions . Select ( d => d . InstalledPath ! ) . ToHashSet ( ) ) ;
325320
326- dbContext . Archives . Remove ( archive ) ;
327- await dbContext . SaveChangesAsync ( ) ;
321+ _dbContext . Archives . Remove ( archive ) ;
322+ await _dbContext . SaveChangesAsync ( ) ;
328323
329324 messageProgress . Report ( $ "Uninstalled { archive . ArchiveName } ") ;
330325 percentageProgress . Report ( index * increment ) ;
@@ -426,12 +421,11 @@ public async Task UpdateInstalledAssetDetailsAsync(TreeNode? selectedItem)
426421 return ;
427422
428423 OnPropertyChanged ( nameof ( UninstallButtonEnabled ) ) ;
429- await using var dbContext = await _dbContextFactory . CreateDbContextAsync ( ) ;
430- var archive = await dbContext . Archives . FindAsync ( selectedItem . DbId ) ;
424+ var archive = await _dbContext . Archives . FindAsync ( selectedItem . DbId ) ;
431425 if ( archive is null )
432426 return ;
433427
434- var files = await dbContext . AssetFiles . Where ( d => d . ArchiveId == archive . Id ) . ToListAsync ( ) ;
428+ var files = await _dbContext . AssetFiles . Where ( d => d . ArchiveId == archive . Id ) . ToListAsync ( ) ;
435429 var details = new List < string >
436430 {
437431 $ "File: { Path . GetFileName ( archive . ArchiveName ) } ",
0 commit comments