namespace HStation.Service.Assets
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public class DbFirstHelper
|
{
|
/// <summary>
|
/// 初始化
|
/// </summary>
|
public static bool Initial(out string msg)
|
{
|
msg = string.Empty;
|
try
|
{
|
var connectConfig = HStation.Assets.ConfigHelper.DefaultConnectionConfig;
|
if (connectConfig == null)
|
{
|
msg = "连接配置初始化失败";
|
return false;
|
}
|
|
connectConfig.ConfigureExternalServices = new ConfigureExternalServices()
|
{
|
EntityService = (property, column) =>
|
{
|
//除主键外其他列都可空
|
if (!column.IsPrimarykey)
|
{
|
column.IsNullable = true;
|
}
|
if (column.DataType == StaticConfig.CodeFirst_BigString)
|
{
|
if (Settings.Assets.XhsParasHelper.Xhs.DataBase.DbType == HStation.Assets.DbType.PostgreSql)
|
{
|
column.DataType = "character varying";
|
}
|
}
|
}
|
};
|
|
var typeList = new List<Type>()
|
{
|
typeof(Entity.AssetsPumpGroup),
|
typeof(Entity.AssetsPumpGroupAndMainMap),
|
typeof(Entity.AssetsPumpMain),
|
typeof(Entity.AssetsPumpMainAndPartMap),
|
typeof(Entity.AssetsPumpPartMain),
|
typeof(Entity.AssetsPumpPropContent),
|
typeof(Entity.AssetsPumpSeries),
|
typeof(Entity.AssetsPumpType),
|
typeof(Entity.AssetsValveMain),
|
typeof(Entity.AssetsValveSeries),
|
typeof(Entity.AssetsElbowMain),
|
typeof(Entity.AssetsElbowSeries),
|
typeof(Entity.AssetsThreelinkMain),
|
typeof(Entity.AssetsThreelinkSeries),
|
typeof(Entity.AssetsFourlinkMain),
|
typeof(Entity.AssetsFourlinkSeries),
|
typeof(Entity.AssetsPipeMain),
|
typeof(Entity.AssetsPipeSeries),
|
typeof(Entity.AssetsSprinklerMain),
|
typeof(Entity.AssetsSprinklerSeries),
|
typeof(Entity.AssetsManufacturer),
|
typeof(Entity.AssetsManufacturerMapping),
|
typeof(Entity.AssetsUserDefined),
|
typeof(Entity.AssetsPipeRoughnessCoefficient),
|
typeof(Entity.AssetsPumpTypeSeriesMap),
|
};
|
|
using (var db = new SqlSugarClient(connectConfig))
|
{
|
//设置字符串默认长度
|
//db.CodeFirst.SetStringDefaultLength(250);
|
//db.CodeFirst.SetStringDefaultLength(int.MaxValue);
|
//建库:如果不存在创建数据库存在不会重复创建 createdb;注意 :Oracle和个别国产库需不支持该方法,需要手动建库
|
db.DbMaintenance.CreateDatabase();
|
db.CodeFirst.InitTables
|
(
|
typeList.ToArray()
|
);
|
}
|
|
return true;
|
}
|
catch (Exception ex)
|
{
|
msg = ex.Message;
|
return false;
|
}
|
}
|
}
|
}
|