RHDWContext.cs 4.2 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<TxInfo> TxInfos { get; set; }
  37. public DbSet<SatInfo> SatInfos { get; set; }
  38. public DbSet<SigInfo> SigInfos { get; set; }
  39. public DbSet<SigDelay> SigDelays { get; set; }
  40. public DbSet<TargetInfo> TargetInfos { get; set; }
  41. public DbSet<SysSetings> SysSetings { get; set; }
  42. }
  43. /// <summary>
  44. /// 分区表上下文(id为long)
  45. /// </summary>
  46. public class RHDWPartContext : DbContext
  47. {
  48. public static RHDWPartContext GetContext(string dbFile, bool createDb = false)
  49. {
  50. if (!File.Exists(dbFile) && !createDb)
  51. {
  52. return null;
  53. }
  54. var connectionString = $@"Data Source={dbFile}";
  55. SQLiteConnection con = new SQLiteConnection(connectionString);
  56. return new RHDWPartContext(con);
  57. }
  58. public static RHDWPartContext GetContext(DateTime partTime, bool createDb = false, string prefix = "")
  59. {
  60. var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart");
  61. var dayFile = Path.Combine(dir, $@"{partTime.Year}\{prefix}{partTime:MMdd}.db");
  62. if (!File.Exists(dayFile) && !createDb)
  63. {
  64. return null;
  65. }
  66. var connectionString = $@"Data Source=|DataDirectory|\DbPart\{partTime.Year}\{prefix}{partTime:MMdd}.db";
  67. SQLiteConnection con = new SQLiteConnection(connectionString);
  68. return new RHDWPartContext(con);
  69. }
  70. private RHDWPartContext(DbConnection con)
  71. : base(con, true)
  72. {
  73. }
  74. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  75. {
  76. this.Database.Log = msg =>
  77. {
  78. };
  79. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  80. modelBuilder.Configurations.AddFromAssembly(typeof(RHDWPartContext).Assembly);
  81. var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<RHDWPartContext>(modelBuilder);
  82. Database.SetInitializer(sqliteConnectionInitializer);
  83. base.OnModelCreating(modelBuilder);
  84. }
  85. public DbSet<StationRes> StationRes { get; set; }
  86. public DbSet<CxRes> CxRes { get; set; }
  87. public DbSet<CgRes> CgRes { get; set; }
  88. public DbSet<CgXgfRes> CgXgfRes { get; set; }
  89. public DbSet<PosRes> PosRes { get; set; }
  90. public DbSet<CheckRes> CheckRes { get; set; }
  91. }
  92. public class SqliteConfiguration : DbConfiguration
  93. {
  94. public SqliteConfiguration()
  95. {
  96. DbInterception.Add(new SqliteInterceptor());//拦截器
  97. SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
  98. SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
  99. SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
  100. }
  101. }
  102. }