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
{
///
/// Initialisiert die Datenbank
///
public class DbInitializer
{
private readonly LocalDbContext _context;
///
/// Erstellt eine Isntanz
///
/// Instanz eines DbContext
public DbInitializer(LocalDbContext context)
{
_context = (LocalDbContext)context;
}
///
/// Befüllen der DB mit Standard-Werten
///
/// true wenn erfolgreich, false sonst
public async Task 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;
}
}
}