SimulationContext.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using SQLite.CodeFirst;
  8. using System.Data.Entity.ModelConfiguration.Conventions;
  9. using System.Data.Entity.Infrastructure.Interception;
  10. using DataSimulation.Repostory.Model;
  11. using System.IO;
  12. using System.Data.Common;
  13. using System.Data.Entity.Infrastructure;
  14. using System.Reflection;
  15. using System.Data.SQLite.EF6;
  16. using System.Data.SQLite;
  17. using System.Data.Entity.Core.Common;
  18. using System.Data.Entity.Core.EntityClient;
  19. using static System.Net.WebRequestMethods;
  20. using DataSimulation.Dto.Model;
  21. namespace DataSimulation.Repostory.EFContext
  22. {
  23. /// <summary>
  24. /// 基础表上下文(id为int)
  25. /// </summary>
  26. public class SimulationContext : DbContext
  27. {
  28. public SimulationContext() : base("DbCon") //配置使用的连接名
  29. {
  30. }
  31. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  32. {
  33. this.Database.Log = msg =>
  34. {
  35. };
  36. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  37. modelBuilder.Configurations.AddFromAssembly(typeof(SimulationContext).Assembly);//自动加载SqliteConfiguration
  38. var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<SimulationContext>(modelBuilder);
  39. Database.SetInitializer(sqliteConnectionInitializer);
  40. DbInterception.Add(new SqliteInterceptor());//拦截器
  41. base.OnModelCreating(modelBuilder);
  42. }
  43. public DbSet<SatInfo> SatInfos { get; set; }
  44. public DbSet<AntInfo> AntInfos { get; set; }
  45. public DbSet<RefInfo> RefInfos { get; set; }
  46. public DbSet<XlInfo> XlInfos { get; set; }
  47. public DbSet<SimulationInfo> SimulationInfos { get; set; }
  48. public DbSet<TaskInfo> TaskInfos { set; get; }
  49. public DbSet<SysSetings> SysSetings { get; set; }
  50. }
  51. /// <summary>
  52. /// 分区表上下文(id为long)
  53. /// </summary>
  54. //[DbConfigurationType(typeof(SqliteConfiguration))]
  55. public class SimulationPartContext : DbContext
  56. {
  57. public static SimulationPartContext GetContext(string dbFile)
  58. {
  59. var connectionString = $@"Data Source={dbFile}";
  60. SQLiteConnection con = new SQLiteConnection(connectionString);
  61. return new SimulationPartContext(con);
  62. }
  63. public static SimulationPartContext GetContext(DateTime partTime)
  64. {
  65. var connectionString = $@"Data Source=|DataDirectory|\DbPart\{partTime.Year}\{partTime:MMdd}.db";
  66. SQLiteConnection con = new SQLiteConnection(connectionString);
  67. return new SimulationPartContext(con);
  68. }
  69. private SimulationPartContext(DbConnection con)
  70. : base(con, true)
  71. {
  72. }
  73. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  74. {
  75. this.Database.Log = msg =>
  76. {
  77. };
  78. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  79. modelBuilder.Configurations.AddFromAssembly(typeof(SimulationPartContext).Assembly);
  80. var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<SimulationPartContext>(modelBuilder);
  81. Database.SetInitializer(sqliteConnectionInitializer);
  82. DbInterception.Add(new SqliteInterceptor());//拦截器
  83. base.OnModelCreating(modelBuilder);
  84. }
  85. public DbSet<XlInfo> XlInfos { set; get; }
  86. //public DbSet<StationRes> StationRes { get; set; }
  87. //public DbSet<CxRes> CxRes { get; set; }
  88. //public DbSet<CgRes> CgRes { get; set; }
  89. //public DbSet<CgXgfRes> CgXgfRes { get; set; }
  90. //public DbSet<PosRes> PosRes { get; set; }
  91. }
  92. public class SqliteConfiguration : DbConfiguration
  93. {
  94. public SqliteConfiguration()
  95. {
  96. SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
  97. SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
  98. SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
  99. }
  100. }
  101. }