MainDBContext.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. namespace EVCB_OCPP.Domain
  2. {
  3. using EVCB_OCPP.Domain.Models.Database;
  4. using System;
  5. using System.Data.Entity;
  6. using System.Data.Entity.Validation;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. public class MainDBContext : DbContext
  11. {
  12. // 您的內容已設定為使用應用程式組態檔 (App.config 或 Web.config)
  13. // 中的 'MainDBContext' 連接字串。根據預設,這個連接字串的目標是
  14. // 您的 LocalDb 執行個體上的 'EVCB_OCPP.Domain.MainDBContext' 資料庫。
  15. //
  16. // 如果您的目標是其他資料庫和 (或) 提供者,請修改
  17. // 應用程式組態檔中的 'MainDBContext' 連接字串。
  18. public MainDBContext()
  19. : base("name=MainDBContext")
  20. {
  21. this.Configuration.LazyLoadingEnabled = false;
  22. this.Database.CommandTimeout = 180;
  23. }
  24. /// <summary>
  25. /// DB coneection set
  26. /// </summary>
  27. public MainDBContext(string conn)
  28. {
  29. this.Database.Connection.ConnectionString = conn;
  30. this.Configuration.LazyLoadingEnabled = false;
  31. this.Database.CommandTimeout = 180;
  32. }
  33. // 針對您要包含在模型中的每種實體類型新增 DbSet。如需有關設定和使用
  34. // Code First 模型的詳細資訊,請參閱 http://go.microsoft.com/fwlink/?LinkId=390109。
  35. public virtual DbSet<OCMF> OCMF { get; set; }
  36. public virtual DbSet<ConnectorStatus> ConnectorStatus { get; set; }
  37. public virtual DbSet<Customer> Customer { get; set; }
  38. public virtual DbSet<Machine> Machine { get; set; }
  39. public virtual DbSet<MachineConfiguration> MachineConfiguration { get; set; }
  40. public virtual DbSet<MachineError> MachineError { get; set; }
  41. public virtual DbSet<MachineOperateRecord> MachineOperateRecord { get; set; }
  42. public virtual DbSet<MachineVersionFile> MachineVersionFile { get; set; }
  43. public virtual DbSet<ServerMessage> ServerMessage { get; set; }
  44. public virtual DbSet<TransactionRecord> TransactionRecord { get; set; }
  45. public virtual DbSet<UploadFile> UploadFile { get; set; }
  46. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  47. {
  48. modelBuilder.Entity<Machine>().Property(x => x.RatedPower).HasPrecision(6, 2);
  49. modelBuilder.Entity<Machine>().Property(x => x.Longitude).HasPrecision(10, 6);
  50. modelBuilder.Entity<Machine>().Property(x => x.Latitude).HasPrecision(10, 6);
  51. modelBuilder.Entity<ConnectorStatus>().Property(x => x.TotalEnergy).HasPrecision(10, 2);
  52. base.OnModelCreating(modelBuilder);
  53. }
  54. public override int SaveChanges()
  55. {
  56. try
  57. {
  58. return base.SaveChanges();
  59. }
  60. catch (DbEntityValidationException ex)
  61. {
  62. var errorMessages = ex.EntityValidationErrors
  63. .SelectMany(x => x.ValidationErrors)
  64. .Select(x => x.ErrorMessage);
  65. // Join the list to a single string.
  66. var fullErrorMessage = string.Join("; ", errorMessages);
  67. // Combine the original exception message with the new one.
  68. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
  69. // Throw a new DbEntityValidationException with the improved exception message.
  70. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
  71. }
  72. }
  73. public override Task<int> SaveChangesAsync(CancellationToken cancellationToken)
  74. {
  75. try
  76. {
  77. return base.SaveChangesAsync(cancellationToken);
  78. }
  79. catch (DbEntityValidationException ex)
  80. {
  81. var errorMessages = ex.EntityValidationErrors
  82. .SelectMany(x => x.ValidationErrors)
  83. .Select(x => x.ErrorMessage);
  84. // Join the list to a single string.
  85. var fullErrorMessage = string.Join("; ", errorMessages);
  86. // Combine the original exception message with the new one.
  87. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
  88. // Throw a new DbEntityValidationException with the improved exception message.
  89. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
  90. }
  91. }
  92. }
  93. }