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;
}
///
/// DB coneection set
///
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 { get; set; }
public virtual DbSet ConnectorStatus { get; set; }
public virtual DbSet Customer { get; set; }
public virtual DbSet Machine { get; set; }
public virtual DbSet MachineConfiguration { get; set; }
public virtual DbSet MachineError { get; set; }
public virtual DbSet MachineOperateRecord { get; set; }
public virtual DbSet MachineVersionFile { get; set; }
public virtual DbSet ServerMessage { get; set; }
public virtual DbSet TransactionRecord { get; set; }
public virtual DbSet UploadFile { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().Property(x => x.RatedPower).HasPrecision(6, 2);
modelBuilder.Entity().Property(x => x.Longitude).HasPrecision(10, 6);
modelBuilder.Entity().Property(x => x.Latitude).HasPrecision(10, 6);
modelBuilder.Entity().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 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);
}
}
}
}