RHDWContext.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using SQLite.CodeFirst;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Common;
  5. using System.Data.Entity;
  6. using System.Data.Entity.Core.Common;
  7. using System.Data.Entity.Infrastructure;
  8. using System.Data.Entity.Infrastructure.Interception;
  9. using System.Data.Entity.ModelConfiguration.Conventions;
  10. using System.Data.Entity.Validation;
  11. using System.Data.SQLite;
  12. using System.Data.SQLite.EF6;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text.RegularExpressions;
  16. using XdCxRhDW.Entity;
  17. namespace XdCxRhDW.Repostory
  18. {
  19. public class RHDWLogContext : DbContext
  20. {
  21. public RHDWLogContext() : base("LogDbCon") //配置使用的连接名
  22. {
  23. }
  24. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  25. {
  26. this.Database.Log = msg =>
  27. {
  28. };
  29. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  30. modelBuilder.Configurations.AddFromAssembly(typeof(RHDWLogContext).Assembly);//自动加载Entity-Type
  31. var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<RHDWLogContext>(modelBuilder);
  32. Database.SetInitializer(sqliteConnectionInitializer);
  33. base.OnModelCreating(modelBuilder);
  34. }
  35. public DbSet<LogRes> LogRes { set; get; }
  36. }
  37. /// <summary>
  38. /// 基础表上下文(id为int)
  39. /// </summary>
  40. public class RHDWContext : DbContext
  41. {
  42. public RHDWContext() : base("DbCon") //配置使用的连接名
  43. {
  44. }
  45. //protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
  46. //{
  47. // var tables = this.Database.SqlQuery<string>("select name from sqlite_master where type='table' and name not like 'sqlite%'").ToList();
  48. // return base.ValidateEntity(entityEntry, items);
  49. //}
  50. //protected override bool ShouldValidateEntity(DbEntityEntry entityEntry)
  51. //{
  52. // return base.ShouldValidateEntity(entityEntry);
  53. //}
  54. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  55. {
  56. this.Database.Log = msg =>
  57. {
  58. };
  59. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  60. modelBuilder.Configurations.AddFromAssembly(typeof(RHDWContext).Assembly);//自动加载Entity-Type
  61. var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<RHDWContext>(modelBuilder);
  62. Database.SetInitializer(sqliteConnectionInitializer);
  63. base.OnModelCreating(modelBuilder);
  64. }
  65. public DbSet<XlInfo> XlInfos { set; get; }
  66. public DbSet<TaskInfo> TaskInfos { set; get; }
  67. public DbSet<TaskSig> TaskSigs { set; get; }
  68. public DbSet<TxInfo> TxInfos { get; set; }
  69. public DbSet<SatInfo> SatInfos { get; set; }
  70. public DbSet<FixedStation> FixedStation { get; set; }
  71. public DbSet<SigInfo> SigInfos { get; set; }
  72. public DbSet<SigDelay> SigDelays { get; set; }
  73. public DbSet<TargetInfo> TargetInfos { get; set; }
  74. public DbSet<SysSetings> SysSetings { get; set; }
  75. }
  76. /// <summary>
  77. /// 分区表上下文(id为long)
  78. /// </summary>
  79. public class RHDWPartContext : DbContext
  80. {
  81. public static RHDWPartContext GetContext(string dbFile, bool createDb = false)
  82. {
  83. if (!File.Exists(dbFile) && !createDb)
  84. {
  85. return null;
  86. }
  87. var connectionString = $@"Data Source={dbFile}";
  88. SQLiteConnection con = new SQLiteConnection(connectionString);
  89. return new RHDWPartContext(con);
  90. }
  91. public static RHDWPartContext GetContext(DateTime partTime, bool createDb = false, string prefix = "")
  92. {
  93. var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart");
  94. var dayFile = Path.Combine(dir, $@"{partTime.Year}\{prefix}{partTime:MMdd}.db");
  95. if (!File.Exists(dayFile) && !createDb)
  96. {
  97. return null;
  98. }
  99. var connectionString = $@"Data Source=|DataDirectory|\DbPart\{partTime.Year}\{prefix}{partTime:MMdd}.db";
  100. SQLiteConnection con = new SQLiteConnection(connectionString);
  101. return new RHDWPartContext(con);
  102. }
  103. private RHDWPartContext(DbConnection con)
  104. : base(con, true)
  105. {
  106. }
  107. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  108. {
  109. this.Database.Log = msg =>
  110. {
  111. };
  112. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  113. modelBuilder.Configurations.AddFromAssembly(typeof(RHDWPartContext).Assembly);
  114. var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<RHDWPartContext>(modelBuilder);
  115. Database.SetInitializer(sqliteConnectionInitializer);
  116. base.OnModelCreating(modelBuilder);
  117. }
  118. public DbSet<StationRes> StationRes { get; set; }
  119. public DbSet<CxRes> CxRes { get; set; }
  120. public DbSet<CgRes> CgRes { get; set; }
  121. public DbSet<CgXgfRes> CgXgfRes { get; set; }
  122. public DbSet<PosRes> PosRes { get; set; }
  123. public DbSet<CheckRes> CheckRes { get; set; }
  124. }
  125. public class SqliteConfiguration : DbConfiguration
  126. {
  127. public SqliteConfiguration()
  128. {
  129. DbInterception.Add(new SqliteInterceptor());//拦截器
  130. SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
  131. SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
  132. SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
  133. }
  134. }
  135. }