MainDBContext.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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<ConnectorStatus> ConnectorStatus { get; set; }
  36. public virtual DbSet<Customer> Customer { get; set; }
  37. public virtual DbSet<Machine> Machine { get; set; }
  38. public virtual DbSet<MachineConfiguration> MachineConfiguration { get; set; }
  39. public virtual DbSet<MachineError> MachineError { get; set; }
  40. public virtual DbSet<MachineOperateRecord> MachineOperateRecord { get; set; }
  41. public virtual DbSet<MachineVersionFile> MachineVersionFile { get; set; }
  42. public virtual DbSet<ServerMessage> ServerMessage { get; set; }
  43. public virtual DbSet<TransactionRecord> TransactionRecord { get; set; }
  44. public virtual DbSet<UploadFile> UploadFile { get; set; }
  45. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  46. {
  47. modelBuilder.Entity<Machine>().Property(x => x.Longitude).HasPrecision(10, 6);
  48. modelBuilder.Entity<Machine>().Property(x => x.Latitude).HasPrecision(10, 6);
  49. base.OnModelCreating(modelBuilder);
  50. }
  51. public override int SaveChanges()
  52. {
  53. try
  54. {
  55. return base.SaveChanges();
  56. }
  57. catch (DbEntityValidationException ex)
  58. {
  59. var errorMessages = ex.EntityValidationErrors
  60. .SelectMany(x => x.ValidationErrors)
  61. .Select(x => x.ErrorMessage);
  62. // Join the list to a single string.
  63. var fullErrorMessage = string.Join("; ", errorMessages);
  64. // Combine the original exception message with the new one.
  65. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
  66. // Throw a new DbEntityValidationException with the improved exception message.
  67. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
  68. }
  69. }
  70. public override Task<int> SaveChangesAsync(CancellationToken cancellationToken)
  71. {
  72. try
  73. {
  74. return base.SaveChangesAsync(cancellationToken);
  75. }
  76. catch (DbEntityValidationException ex)
  77. {
  78. var errorMessages = ex.EntityValidationErrors
  79. .SelectMany(x => x.ValidationErrors)
  80. .Select(x => x.ErrorMessage);
  81. // Join the list to a single string.
  82. var fullErrorMessage = string.Join("; ", errorMessages);
  83. // Combine the original exception message with the new one.
  84. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
  85. // Throw a new DbEntityValidationException with the improved exception message.
  86. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
  87. }
  88. }
  89. }
  90. }