using SqlSugar;
|
|
namespace Yw.Service
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public class DbFirstHelper
|
{
|
/// <summary>
|
/// 初始化
|
/// </summary>
|
public static bool Initial(out string msg)
|
{
|
var settings = Yw.Settings.MigrationParasHelper.Migration;
|
msg = string.Empty;
|
try
|
{
|
#region 基础模块
|
|
var basicConnectConfig = Yw.Migration.ConfigHelper.GetConnectionConfig(settings.Basic.Dest.DbType, settings.Basic.Dest.ConnectString);
|
if (basicConnectConfig == null)
|
{
|
msg = "基础模块连接配置初始化失败";
|
return false;
|
}
|
basicConnectConfig.ConfigureExternalServices = new ConfigureExternalServices()
|
{
|
EntityService = (property, column) =>
|
{
|
//除主键外其他列都可空
|
if (!column.IsPrimarykey)
|
{
|
column.IsNullable = true;
|
}
|
if (column.DataType == StaticConfig.CodeFirst_BigString)
|
{
|
if (settings.Basic.Dest.DbType == Yw.Migration.DbType.PostgreSql)
|
{
|
column.DataType = "character varying";
|
}
|
}
|
}
|
};
|
|
var basicTypeList = new List<Type>();
|
//系统类型
|
if (settings.Basic.Config.ContainsType)
|
{
|
basicTypeList.Add(typeof(Yw.Entity.SysModule));
|
basicTypeList.Add(typeof(Yw.Entity.SysType));
|
}
|
//系统分类
|
if (settings.Basic.Config.ContainsCatalog)
|
{
|
basicTypeList.Add(typeof(Yw.Entity.SysCatalog));
|
basicTypeList.Add(typeof(Yw.Entity.SysCatalogOrg));
|
}
|
//系统属性
|
if (settings.Basic.Config.ContainsProp)
|
{
|
}
|
if (basicTypeList.Count > 0)
|
{
|
using (var db = new SqlSugarClient(basicConnectConfig))
|
{
|
//设置字符串默认长度
|
//db.CodeFirst.SetStringDefaultLength(250);
|
//db.CodeFirst.SetStringDefaultLength(int.MaxValue);
|
//建库:如果不存在创建数据库存在不会重复创建 createdb;注意 :Oracle和个别国产库需不支持该方法,需要手动建库
|
db.DbMaintenance.CreateDatabase();
|
db.CodeFirst.InitTables
|
(
|
basicTypeList.ToArray()
|
);
|
}
|
}
|
|
#endregion 基础模块
|
|
return true;
|
}
|
catch (Exception ex)
|
{
|
msg = ex.Message;
|
return false;
|
}
|
}
|
}
|
}
|