RHDWContext.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using SQLite.CodeFirst;
  2. using System;
  3. using System.Data.Common;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Core.Common;
  6. using System.Data.Entity.Infrastructure.Interception;
  7. using System.Data.Entity.ModelConfiguration.Conventions;
  8. using System.Data.SQLite;
  9. using System.Data.SQLite.EF6;
  10. using System.IO;
  11. using System.Text.RegularExpressions;
  12. using XdCxRhDW.Entity;
  13. namespace XdCxRhDW.Repostory
  14. {
  15. /// <summary>
  16. /// 基础表上下文(id为int)
  17. /// </summary>
  18. public class RHDWContext : DbContext
  19. {
  20. public RHDWContext() : base("DbCon") //配置使用的连接名
  21. {
  22. }
  23. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  24. {
  25. this.Database.Log = msg =>
  26. {
  27. };
  28. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  29. modelBuilder.Configurations.AddFromAssembly(typeof(RHDWContext).Assembly);//自动加载Entity-Type
  30. var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<RHDWContext>(modelBuilder);
  31. Database.SetInitializer(sqliteConnectionInitializer);
  32. base.OnModelCreating(modelBuilder);
  33. }
  34. public DbSet<XlInfo> XlInfos { set; get; }
  35. public DbSet<TaskInfo> TaskInfos { set; get; }
  36. public DbSet<TaskSig> TaskSigs { set; get; }
  37. public DbSet<TxInfo> TxInfos { get; set; }
  38. public DbSet<SatInfo> SatInfos { get; set; }
  39. public DbSet<SigInfo> SigInfos { get; set; }
  40. public DbSet<SigDelay> SigDelays { get; set; }
  41. public DbSet<TargetInfo> TargetInfos { get; set; }
  42. public DbSet<SysSetings> SysSetings { get; set; }
  43. }
  44. /// <summary>
  45. /// 分区表上下文(id为long)
  46. /// </summary>
  47. public class RHDWPartContext : DbContext
  48. {
  49. public static RHDWPartContext GetContext(string dbFile, bool createDb = false)
  50. {
  51. if (!File.Exists(dbFile) && !createDb)
  52. {
  53. return null;
  54. }
  55. var connectionString = $@"Data Source={dbFile}";
  56. SQLiteConnection con = new SQLiteConnection(connectionString);
  57. return new RHDWPartContext(con);
  58. }
  59. public static RHDWPartContext GetContext(DateTime partTime, bool createDb = false, string prefix = "")
  60. {
  61. var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart");
  62. var dayFile = Path.Combine(dir, $@"{partTime.Year}\{prefix}{partTime:MMdd}.db");
  63. if (!File.Exists(dayFile) && !createDb)
  64. {
  65. return null;
  66. }
  67. var connectionString = $@"Data Source=|DataDirectory|\DbPart\{partTime.Year}\{prefix}{partTime:MMdd}.db";
  68. SQLiteConnection con = new SQLiteConnection(connectionString);
  69. return new RHDWPartContext(con);
  70. }
  71. private RHDWPartContext(DbConnection con)
  72. : base(con, true)
  73. {
  74. }
  75. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  76. {
  77. this.Database.Log = msg =>
  78. {
  79. };
  80. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  81. modelBuilder.Configurations.AddFromAssembly(typeof(RHDWPartContext).Assembly);
  82. var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<RHDWPartContext>(modelBuilder);
  83. Database.SetInitializer(sqliteConnectionInitializer);
  84. base.OnModelCreating(modelBuilder);
  85. }
  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. public DbSet<CheckRes> CheckRes { get; set; }
  92. }
  93. public class SqliteConfiguration : DbConfiguration
  94. {
  95. public SqliteConfiguration()
  96. {
  97. DbInterception.Add(new SqliteInterceptor());//拦截器
  98. SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
  99. SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
  100. SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
  101. }
  102. }
  103. }