IDbContextProvider.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Application.ATG_Classic_App;
  2. using Microsoft.EntityFrameworkCore;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace Application.ATG_Classic_App
  7. {
  8. public interface IDbContextFactory : IDisposable
  9. {
  10. void AddProvider(IDbContextProvider provider);
  11. T CreateDbContext<T>() where T : DbContext;
  12. }
  13. public interface IDbContextProvider : IDisposable
  14. {
  15. T CreateDbContext<T>() where T : DbContext;
  16. }
  17. public class DefaultDbContextFactory : IDbContextFactory
  18. {
  19. private IDbContextProvider provider;
  20. public void AddProvider(IDbContextProvider provider)
  21. {
  22. this.provider = provider;
  23. }
  24. public T CreateDbContext<T>() where T : DbContext
  25. {
  26. return this.provider.CreateDbContext<T>();
  27. }
  28. public void Dispose()
  29. {
  30. //throw new NotImplementedException();
  31. }
  32. }
  33. public class ClassicAtgDbContextProvider : IDbContextProvider
  34. {
  35. /// <summary>
  36. ///
  37. /// </summary>
  38. /// <param name="options">leave null to create Sqlite database</param>
  39. /// <returns></returns>
  40. public T CreateDbContext<T>() where T : DbContext
  41. {
  42. var options = new DbContextOptionsBuilder<AppDbContext>()
  43. .UseSqlite("Data Source=Application.ATG_Classic_App_database.db")
  44. .Options;
  45. return new AppDbContext(options) as T;
  46. }
  47. public void Dispose()
  48. {
  49. //throw new NotImplementedException();
  50. }
  51. }
  52. }