using Application.ATG_Classic_App; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; namespace Application.ATG_Classic_App { public interface IDbContextFactory : IDisposable { void AddProvider(IDbContextProvider provider); T CreateDbContext<T>() where T : DbContext; } public interface IDbContextProvider : IDisposable { T CreateDbContext<T>() where T : DbContext; } public class DefaultDbContextFactory : IDbContextFactory { private IDbContextProvider provider; public void AddProvider(IDbContextProvider provider) { this.provider = provider; } public T CreateDbContext<T>() where T : DbContext { return this.provider.CreateDbContext<T>(); } public void Dispose() { //throw new NotImplementedException(); } } public class ClassicAtgDbContextProvider : IDbContextProvider { /// <summary> /// /// </summary> /// <param name="options">leave null to create Sqlite database</param> /// <returns></returns> public T CreateDbContext<T>() where T : DbContext { var options = new DbContextOptionsBuilder<AppDbContext>() .UseSqlite("Data Source=Application.ATG_Classic_App_database.db") .Options; return new AppDbContext(options) as T; } public void Dispose() { //throw new NotImplementedException(); } } }