123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- namespace EVCB_OCPP.Domain
- {
- using EVCB_OCPP.Domain.Models.Database;
- using System;
- using System.Data.Entity;
- using System.Data.Entity.Validation;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- public class MainDBContext : DbContext
- {
- // 您的內容已設定為使用應用程式組態檔 (App.config 或 Web.config)
- // 中的 'MainDBContext' 連接字串。根據預設,這個連接字串的目標是
- // 您的 LocalDb 執行個體上的 'EVCB_OCPP.Domain.MainDBContext' 資料庫。
- //
- // 如果您的目標是其他資料庫和 (或) 提供者,請修改
- // 應用程式組態檔中的 'MainDBContext' 連接字串。
- public MainDBContext()
- : base("name=MainDBContext")
- {
- this.Configuration.LazyLoadingEnabled = false;
- this.Database.CommandTimeout = 180;
- }
- /// <summary>
- /// DB coneection set
- /// </summary>
- public MainDBContext(string conn)
- {
- this.Database.Connection.ConnectionString = conn;
- this.Configuration.LazyLoadingEnabled = false;
- this.Database.CommandTimeout = 180;
- }
- // 針對您要包含在模型中的每種實體類型新增 DbSet。如需有關設定和使用
- // Code First 模型的詳細資訊,請參閱 http://go.microsoft.com/fwlink/?LinkId=390109。
- public virtual DbSet<OCMF> OCMF { get; set; }
- public virtual DbSet<ConnectorStatus> ConnectorStatus { get; set; }
- public virtual DbSet<Customer> Customer { get; set; }
- public virtual DbSet<Machine> Machine { get; set; }
- public virtual DbSet<MachineConfiguration> MachineConfiguration { get; set; }
- public virtual DbSet<MachineError> MachineError { get; set; }
- public virtual DbSet<MachineOperateRecord> MachineOperateRecord { get; set; }
- public virtual DbSet<MachineVersionFile> MachineVersionFile { get; set; }
- public virtual DbSet<ServerMessage> ServerMessage { get; set; }
- public virtual DbSet<TransactionRecord> TransactionRecord { get; set; }
- public virtual DbSet<UploadFile> UploadFile { get; set; }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- modelBuilder.Entity<Machine>().Property(x => x.RatedPower).HasPrecision(6, 2);
- modelBuilder.Entity<Machine>().Property(x => x.Longitude).HasPrecision(10, 6);
- modelBuilder.Entity<Machine>().Property(x => x.Latitude).HasPrecision(10, 6);
- modelBuilder.Entity<ConnectorStatus>().Property(x => x.TotalEnergy).HasPrecision(10, 2);
- base.OnModelCreating(modelBuilder);
- }
- public override int SaveChanges()
- {
- try
- {
- return base.SaveChanges();
- }
- catch (DbEntityValidationException ex)
- {
- var errorMessages = ex.EntityValidationErrors
- .SelectMany(x => x.ValidationErrors)
- .Select(x => x.ErrorMessage);
- // Join the list to a single string.
- var fullErrorMessage = string.Join("; ", errorMessages);
- // Combine the original exception message with the new one.
- var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
- // Throw a new DbEntityValidationException with the improved exception message.
- throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
- }
- }
- public override Task<int> SaveChangesAsync(CancellationToken cancellationToken)
- {
- try
- {
- return base.SaveChangesAsync(cancellationToken);
- }
- catch (DbEntityValidationException ex)
- {
- var errorMessages = ex.EntityValidationErrors
- .SelectMany(x => x.ValidationErrors)
- .Select(x => x.ErrorMessage);
- // Join the list to a single string.
- var fullErrorMessage = string.Join("; ", errorMessages);
- // Combine the original exception message with the new one.
- var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
- // Throw a new DbEntityValidationException with the improved exception message.
- throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
- }
- }
-
- }
- }
|