69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace gehGassiApp.Data
|
|
{
|
|
/// <summary>
|
|
/// Initialisiert die Datenbank
|
|
/// </summary>
|
|
public class DbInitializer
|
|
{
|
|
private readonly LocalDbContext _context;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Isntanz
|
|
/// </summary>
|
|
/// <param name="context">Instanz eines DbContext</param>
|
|
public DbInitializer(LocalDbContext context)
|
|
{
|
|
_context = (LocalDbContext)context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Befüllen der DB mit Standard-Werten
|
|
/// </summary>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<bool> SeedAsync()
|
|
{
|
|
var defaultTimeout = _context.Database.GetCommandTimeout();
|
|
|
|
System.Diagnostics.Debug.WriteLine($"Migration-Thread: {Thread.CurrentThread.Name} - {Thread.CurrentThread.ManagedThreadId}");
|
|
|
|
try
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Calling MigrateAsync");
|
|
//_context.Database.ExecuteSqlRaw("PRAGMA busy_timeout = 60000;");
|
|
await _context.Database.MigrateAsync(CancellationToken.None);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var err = ex.Message;
|
|
System.Diagnostics.Debug.WriteLine($"Error Migrating DB: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
//_context.Database.SetCommandTimeout(defaultTimeout);
|
|
}
|
|
|
|
try
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Calling SaveChangesAsync");
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var err = ex.Message;
|
|
System.Diagnostics.Debug.WriteLine($"Error SaveChangesAsync after MigrateAsync: {ex.Message}");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|