You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.7 KiB

4 years ago
using System;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.Entity.Core.EntityClient;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CK.SCP.Models.AppBoxEntity;
using CK.SCP.Models.Enums;
using CK.SCP.Models.UniApiEntity;
using CK.SCP.Utils;
namespace CK.SCP.Models
{
public static class EntitiesFactory
{
static EntitiesFactory()
{
Database.SetInitializer<ScpEntities>(null);
Database.SetInitializer<UniApiEntities>(null);
}
public static ScpEntities CreateScpInstance()
{
try
{
// var strConn = setting == null ? GetEfConnectionString("Wms") : GetEfConnectionString(setting);
var strConn = GetEfConnectionString(GlobalConfig.ScpDatabase);
var db = new ScpEntities(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"ϵͳ�޷����ӵ����ݿ�:{GlobalConfig.ScpDatabase}�����������õķ�����,���ݿ�,�û�������������Ϣ�Ƿ���ȷ��" + Environment.NewLine + ex);
}
}
public static UniApiEntities CreateUniApiInstance()
{
try
{
// var strConn = setting == null ? GetEfConnectionString("UniApi") : GetEfConnectionString(setting);
var strConn = GetEfConnectionString(GlobalConfig.UniApiDatabase);
var db = new UniApiEntities(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"ϵͳ�޷����ӵ����ݿ�:{GlobalConfig.UniApiDatabase}�����������õķ�����,���ݿ�,�û�������������Ϣ�Ƿ���ȷ��" + Environment.NewLine + ex);
}
}
public static AppBoxContext CreateAppBoxInstance()
{
try
{
// var strConn = setting == null ? GetEfConnectionString("UniApi") : GetEfConnectionString(setting);
var strConn = GetEfConnectionString(GlobalConfig.AppBoxDatabase);
var db = new AppBoxContext(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"ϵͳ�޷����ӵ����ݿ�:{GlobalConfig.AppBoxDatabase}�����������õķ�����,���ݿ�,�û�������������Ϣ�Ƿ���ȷ��" + Environment.NewLine + ex);
}
}
public static string GetEfConnectionString(DbSetting dbSetting)
{
var sbConn = new StringBuilder();
if (string.IsNullOrEmpty(dbSetting.))
{
throw new Exception("�����ļ�������������");
}
switch (dbSetting.ݿ)
{
case "SQLServer":
sbConn.Append($"Data source ={dbSetting.��������ַ}");
sbConn.Append(dbSetting.˿ == "0" ? ";" : $",{dbSetting.�˿�};");
sbConn.Append($"Initial catalog = {dbSetting.���ݿ�����};");
sbConn.Append($"User id = {dbSetting.�û���};");
sbConn.Append($"Password = {EncryptHelper.Decrypt(dbSetting.����)};");
sbConn.Append("MultipleActiveResultSets = True;");
sbConn.Append("persist security info = True;");
sbConn.Append("App = EntityFramework;");
break;
case "MySql":
sbConn.Append($"server ={dbSetting.��������ַ};");
sbConn.Append($"port ={dbSetting.�˿�};");
sbConn.Append($"database = {dbSetting.���ݿ�����};");
sbConn.Append($"user id = {dbSetting.�û���};");
sbConn.Append($"password = {EncryptHelper.Decrypt(dbSetting.����)};");
sbConn.Append("persistsecurityinfo =True;");
break;
}
// LogHelper.Write(sbConn.ToString());
return sbConn.ToString();
}
public static void SaveDb(DbContext db)
{
try
{
db.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//����ʵ����֤�쳣
{
var sb = new StringBuilder();
foreach (var error in dbEx.EntityValidationErrors.ToList())
{
error.ValidationErrors.ToList().ForEach(i =>
{
sb.AppendFormat("����{0}���ֶΣ�{1}����Ϣ��{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
});
}
throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "�ֶ���֤ʧ��" + sb.ToString());
}
catch (OptimisticConcurrencyException ex)//������ͻ�쳣
{
Console.WriteLine(ex.ToString());
throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
}
catch (ScpException ex)
{
Console.WriteLine(ex.ToString());
if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
{
var inner = (UpdateException)ex.InnerException;
//Console.WriteLine(inner?.StateEntries[0].EntitySet.Name);
throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
}
else
{
if (ex.InnerException != null) throw ex.InnerException;
}
}
}
}
}