|
@@ -26,7 +26,7 @@ namespace Hotline.Repository.SqlSugar.Extensions
|
|
|
|
|
|
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
|
|
|
{
|
|
|
- DbType = DbType.MySql,
|
|
|
+ DbType = DbType.PostgreSQL,
|
|
|
ConnectionString = configuration.GetConnectionString(dbName),
|
|
|
IsAutoCloseConnection = true,
|
|
|
ConfigureExternalServices = new ConfigureExternalServices
|
|
@@ -98,6 +98,11 @@ namespace Hotline.Repository.SqlSugar.Extensions
|
|
|
.Description;
|
|
|
}
|
|
|
}
|
|
|
+ },
|
|
|
+ MoreSettings = new ConnMoreSettings
|
|
|
+ {
|
|
|
+ PgSqlIsAutoToLower = false,//增删查改支持驼峰表
|
|
|
+ PgSqlIsAutoToLowerCodeFirst = false, // 建表建驼峰表。5.1.3.30
|
|
|
}
|
|
|
},
|
|
|
SetDbAop
|
|
@@ -111,14 +116,13 @@ namespace Hotline.Repository.SqlSugar.Extensions
|
|
|
|
|
|
private static void InitDatabase(ISugarUnitOfWork<HotlineDbContext> context, IConfiguration configuration)
|
|
|
{
|
|
|
- context.Db.DbMaintenance.CreateDatabase();
|
|
|
-
|
|
|
var dbOptions = configuration.GetSection("DatabaseConfiguration").Get<DatabaseOptions>() ?? new DatabaseOptions();
|
|
|
if (dbOptions.ApplyDbMigrations)
|
|
|
{
|
|
|
- var nonTableTypes = new[] { typeof(OrderExtensionEntity), typeof(StepBasicEntity) };
|
|
|
+ context.Db.DbMaintenance.CreateDatabase();
|
|
|
+
|
|
|
var types = typeof(User).Assembly.GetTypes()
|
|
|
- .Where(d => d.GetInterfaces().Any(x => x == typeof(ITable) && !nonTableTypes.Contains(x)))
|
|
|
+ .Where(d => d.GetInterfaces().Any(x => x == typeof(ITable) && !d.IsAbstract))
|
|
|
.Distinct()
|
|
|
.ToArray();
|
|
|
|
|
@@ -141,25 +145,18 @@ namespace Hotline.Repository.SqlSugar.Extensions
|
|
|
if (seedData == null) continue;
|
|
|
|
|
|
var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
|
|
|
- var tableName = context.Db.EntityMaintenance.GetTableName(entityType);
|
|
|
-
|
|
|
- var seedDataTable = seedData.ToList().ToDataTable(tableName);
|
|
|
-
|
|
|
- if (seedDataTable.Columns.Contains(SqlSugarConst.PrimaryKey))
|
|
|
+
|
|
|
+ var entityInfo = context.Db.EntityMaintenance.GetEntityInfo(entityType);
|
|
|
+ if (entityInfo.Columns.Any(d => d.IsPrimarykey))
|
|
|
{
|
|
|
- var storage = context.Db.Storageable(seedDataTable)
|
|
|
- //.SplitInsert(d => !d.Any())
|
|
|
- .WhereColumns(SqlSugarConst.PrimaryKey).ToStorage();
|
|
|
+ var storage = context.Db.StorageableByObject(seedData.ToList()).ToStorage();
|
|
|
storage.AsInsertable.ExecuteCommand();
|
|
|
- //var ignoreUpdate = hasDataMethod.GetCustomAttribute<IgnoreUpdateAttribute>();
|
|
|
- //if (ignoreUpdate == null) storage.AsUpdateable.ExecuteCommand();
|
|
|
}
|
|
|
- else // 没有主键或者不是预定义的主键(有重复的可能)
|
|
|
+ else
|
|
|
{
|
|
|
- var storage = context.Db.Storageable(seedDataTable)
|
|
|
- .SplitDelete(d => true)
|
|
|
- .ToStorage();
|
|
|
- storage.AsInsertable.ExecuteCommand();
|
|
|
+ // 无主键则只进行插入
|
|
|
+ if (!context.Db.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
|
|
|
+ context.Db.InsertableByObject(seedData.ToList()).ExecuteCommand();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -173,6 +170,7 @@ namespace Hotline.Repository.SqlSugar.Extensions
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
{
|
|
|
//Log.Information(sql);
|
|
|
+ //Log.Information(string.Join(',', pars.Select(d => d.Value)));
|
|
|
};
|
|
|
db.Aop.OnError = (exp) =>//SQL报错
|
|
|
{
|