OracleContext.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using DW5S.Entity;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.EntityFrameworkCore.Metadata.Conventions;
  9. using Microsoft.Extensions.Logging;
  10. namespace DW5S.Repostory
  11. {
  12. public class OracleContext : DbContext
  13. {
  14. public OracleContext()
  15. {
  16. //发布为单文件后找不到Microsoft.EntityFrameworkCore.Relational.dll,需要将这个dll拷贝到AddIns中
  17. }
  18. public async Task ExecuteSqlAsync(string sql)
  19. {
  20. await this.Database.ExecuteSqlRawAsync(sql);
  21. }
  22. /// <summary>
  23. /// 执行SQL查询(查询最大ID需要给int?等可空类型,否则会出错)
  24. /// </summary>
  25. /// <typeparam name="T"></typeparam>
  26. /// <param name="sql"></param>
  27. /// <returns></returns>
  28. public async Task<List<T>> SqlQuery<T>(string sql)
  29. {
  30. var res = await this.Database.SqlQueryRaw<T>(sql).ToListAsync();
  31. return res;
  32. }
  33. /// <summary>
  34. /// 更新所有表的序列值为对应表的最大ID(更新后需要重新编译触发器)
  35. /// </summary>
  36. public void UpdateSeq()
  37. {
  38. List<string> tables = new List<string>();
  39. var props = typeof(OracleContext).GetProperties().Where(p => p.PropertyType.Name.StartsWith("DbSet")).ToList();
  40. foreach (var item in props)
  41. {
  42. var tb = item.PropertyType.GetGenericArguments().First().Name.ToUpper();
  43. tables.Add(tb);
  44. }
  45. foreach (var tb in tables)
  46. {
  47. try
  48. {
  49. int? idMax = this.SqlQuery<int?>($"select max(id) from {tb}").Result.FirstOrDefault();
  50. if (idMax == null)
  51. idMax = 1;
  52. string sql = $"DROP SEQUENCE SQ_{tb}";
  53. this.ExecuteSqlAsync(sql).Wait();
  54. sql = $"CREATE SEQUENCE SQ_{tb} INCREMENT BY 1 START WITH {idMax}";
  55. this.ExecuteSqlAsync(sql).Wait();
  56. }
  57. catch (Exception ex)
  58. {
  59. Console.WriteLine(ex.Message);
  60. }
  61. }
  62. }
  63. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  64. {
  65. var conStr = ConfigurationManager.ConnectionStrings["OracleCon"].ToString();
  66. Console.WriteLine($"conStr={conStr}");
  67. optionsBuilder.UseOracle(conStr,
  68. options =>
  69. {
  70. //options.MigrationsAssembly("Ips.Sps.Repostory");
  71. options.UseOracleSQLCompatibility("11");//11=Oracle11g,12=Oracle12c
  72. })
  73. //.UseSnakeCaseNamingConvention()//FullName->full_name
  74. //.UseLowerCaseNamingConvention()//FullName->fullname
  75. //.UseUpperSnakeCaseNamingConvention()// FullName->FULL_NAME
  76. .UseUpperCaseNamingConvention();//FullName->FULLNAME
  77. //sql语句输出到日志文件
  78. var loggerFactory = new LoggerFactory();
  79. loggerFactory.AddProvider(new SqlLoggerProvider());
  80. optionsBuilder.UseLoggerFactory(loggerFactory);
  81. base.OnConfiguring(optionsBuilder);
  82. }
  83. protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
  84. {
  85. //EFCore默认表名会带上复数形式,通过这句话删除复数约定
  86. configurationBuilder.Conventions.Remove(typeof(TableNameFromDbSetConvention));
  87. }
  88. protected override void OnModelCreating(ModelBuilder modelBuilder)
  89. {
  90. //在Oracle11g中列名、索引、字段、表名、序列、触发器等名称长度均不能超过30个字符
  91. //自动生成的超出30个字符的需要在下面自定义
  92. modelBuilder.Entity<CgRes>().HasOne(p => p.StationRes).WithMany()
  93. .HasConstraintName("FK_CGRES_STATIONRESID");
  94. modelBuilder.Entity<SampleInfo>().HasOne(p => p.SigInfo).WithOne()
  95. .HasConstraintName("FK_SAMPLEINFO_SIGINFOID");
  96. modelBuilder.Entity<PosRes>().HasOne(p => p.StationRes).WithOne()
  97. .HasConstraintName("FK_POSRES_STATIONRESID");
  98. modelBuilder.Entity<PosRes>().HasOne(p => p.TargetInfo).WithOne()
  99. .HasConstraintName("FK_POSRES_TARGETINFOID");
  100. modelBuilder.Entity<RefTaskFreq>().HasOne(p => p.SampleInfo).WithOne()
  101. .HasConstraintName("FK_REFTASKFREQ_SAMPLEINFOID");
  102. modelBuilder.Entity<RefTaskFreq>().HasOne(p => p.SatInfo).WithOne()
  103. .HasConstraintName("FK_REFTASKFREQ_SATINFOID");
  104. modelBuilder.Entity<LogRes>().HasIndex(p => new { p.Module, p.LogTime, p.LogType })
  105. .HasDatabaseName("IX_LOGRES_MODULE_TIME_TYPE");
  106. modelBuilder.Entity<RefCgRes>().HasIndex(p => new { p.FileTime, p.FrequpHz, p.YbSnr })
  107. .HasDatabaseName("IX_REFCGRES_TIME_FREQ_SNR");
  108. modelBuilder.Entity<CgRes>().HasIndex(p => new { p.TaskID, p.SigTime, p.FrequpHz })
  109. .HasDatabaseName("IX_CGRES_TASKID_TIME_FREQ");
  110. modelBuilder.Entity<PosRes>().HasIndex(p => new { p.TaskID, p.SigTime, p.FrequpHz })
  111. .HasDatabaseName("IX_POSRES_TASKID_TIME_FREQ");
  112. }
  113. public DbSet<XlInfo> XlInfos { set; get; }
  114. public DbSet<TaskInfo> TaskInfos { set; get; }
  115. public DbSet<TaskSig> TaskSigs { set; get; }
  116. public DbSet<TxInfo> TxInfos { get; set; }
  117. public DbSet<SatInfo> SatInfos { get; set; }
  118. public DbSet<FixedStation> FixedStation { get; set; }
  119. public DbSet<SigInfo> SigInfos { get; set; }
  120. public DbSet<SigDelay> SigDelays { get; set; }
  121. public DbSet<TargetInfo> TargetInfos { get; set; }
  122. public DbSet<SysSetings> SysSetings { get; set; }
  123. public DbSet<TaskRunnningInfo> TaskRunnningInfos { get; set; }
  124. public DbSet<SampleInfo> SampleInfos { get; set; }
  125. public DbSet<RefTaskFreq> RefTaskFreqs { get; set; }
  126. /*以下是Range分区表的DbSet*/
  127. public DbSet<AdRes> AdRes { get; set; }
  128. public DbSet<LogRes> LogRes { set; get; }
  129. public DbSet<StationRes> StationRes { get; set; }
  130. public DbSet<CxRes> CxRes { get; set; }
  131. public DbSet<CgRes> CgRes { get; set; }
  132. public DbSet<RefCgRes> RefCgRes { get; set; }
  133. public DbSet<CgXgfRes> CgXgfRes { get; set; }
  134. public DbSet<PosRes> PosRes { get; set; }
  135. public DbSet<CheckRes> CheckRes { get; set; }
  136. }
  137. }