|
|
@ -145,76 +145,76 @@ public abstract class ModuleBase<T> : AbpModule where T : AbpModule |
|
|
|
|
|
|
|
public virtual void CreateDatabase<TEfCoreDbContext>(ApplicationInitializationContext context) where TEfCoreDbContext : IEfCoreDbContext |
|
|
|
{ |
|
|
|
Console.WriteLine($"应用版本:{Assembly.GetEntryAssembly().GetFileVersion()}"); |
|
|
|
//if (!context.GetEnvironment().IsDevelopment())
|
|
|
|
//Console.WriteLine($"应用版本:{Assembly.GetEntryAssembly().GetFileVersion()}");
|
|
|
|
////if (!context.GetEnvironment().IsDevelopment())
|
|
|
|
////{
|
|
|
|
//// Console.WriteLine($"生产模式不执行数据库初始化");
|
|
|
|
//// return;
|
|
|
|
////}
|
|
|
|
//using var scope = context.ServiceProvider.CreateScope();
|
|
|
|
//var contextName = typeof(TEfCoreDbContext).Name;
|
|
|
|
////var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
|
|
|
|
////using var uow = uowManager.Begin();
|
|
|
|
//var provider = scope.ServiceProvider.GetRequiredService<IDbContextProvider<TEfCoreDbContext>>();
|
|
|
|
////using var dbContext = provider.GetDbContextAsync().Result;
|
|
|
|
//using var dbContext = scope.ServiceProvider.GetRequiredService(typeof(TEfCoreDbContext)) as DbContext;
|
|
|
|
//dbContext.Database.SetCommandTimeout(TimeSpan.FromMinutes(10));
|
|
|
|
//var dbCreator = dbContext.GetService<IRelationalDatabaseCreator>() as RelationalDatabaseCreator;
|
|
|
|
//var sql = dbCreator.GenerateCreateScript();
|
|
|
|
//sql = dbContext.Database.ProviderName.Contains("SqlServer") ? Regex.Replace(sql, ";\\s+GO\\s", " ") : sql;
|
|
|
|
//var md5 = sql.Md5();
|
|
|
|
//var path = Path.Combine(Directory.GetCurrentDirectory(), "scripts");
|
|
|
|
//Directory.CreateDirectory(path);
|
|
|
|
//using var sw = File.CreateText(Path.Combine(path, $"db.{dbContext.Database.ProviderName}.{contextName}.sql"));
|
|
|
|
//sw.Write(sql);
|
|
|
|
//Console.WriteLine($"{contextName} 初始化开始");
|
|
|
|
//Console.WriteLine($"ConnectionString:{dbContext.Database.GetConnectionString()}");
|
|
|
|
////创建数据库
|
|
|
|
//if (!dbCreator.Exists())
|
|
|
|
//{
|
|
|
|
// Console.WriteLine($"生产模式不执行数据库初始化");
|
|
|
|
// return;
|
|
|
|
// dbCreator.Create();
|
|
|
|
// var createSql = "CREATE TABLE EFDbContext(Id varchar(255) NOT NULL,Hash varchar(255) NOT NULL,PRIMARY KEY (Id));";
|
|
|
|
// dbContext.Database.ExecuteSqlRaw(createSql);
|
|
|
|
//}
|
|
|
|
//// 查询当前DbContext是否已经初始化
|
|
|
|
//using var conn = dbContext.Database.GetDbConnection();
|
|
|
|
//var cmd = conn.CreateCommand();
|
|
|
|
//conn.Open();
|
|
|
|
//cmd.CommandText = $"SELECT Hash FROM EFDbContext where Id='{contextName}'";
|
|
|
|
//var hash = cmd.ExecuteScalar();
|
|
|
|
////conn.Close();
|
|
|
|
////conn.Dispose();
|
|
|
|
//if (hash == null)
|
|
|
|
//{
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
// //dbContext.Database.BeginTransaction();
|
|
|
|
// dbContext.Database.ExecuteSqlRaw(sql);
|
|
|
|
// dbContext.Database.ExecuteSqlRaw($"INSERT INTO EFDbContext VALUES ('{contextName}', '{md5}');");
|
|
|
|
// context.ServiceProvider
|
|
|
|
// .GetRequiredService<IDataSeeder>()
|
|
|
|
// .SeedAsync()
|
|
|
|
// .Wait();
|
|
|
|
// //dbContext.Database.CommitTransaction();
|
|
|
|
// Console.WriteLine($"{contextName} 初始化成功");
|
|
|
|
// }
|
|
|
|
// catch (Exception ex)
|
|
|
|
// {
|
|
|
|
// //dbContext.Database.RollbackTransaction();
|
|
|
|
// var message = $"{contextName} 初始化失败:{ex.Message}";
|
|
|
|
// Console.WriteLine(message);
|
|
|
|
// Console.WriteLine(ex.ToString());
|
|
|
|
// throw new Exception(message, ex);
|
|
|
|
// }
|
|
|
|
// finally
|
|
|
|
// {
|
|
|
|
// Console.WriteLine($"{contextName} 初始化结束");
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//else
|
|
|
|
//{
|
|
|
|
// Console.WriteLine($"{contextName} 数据库结构{(hash.ToString() == md5 ? "正常" : "已过时")}");
|
|
|
|
//}
|
|
|
|
using var scope = context.ServiceProvider.CreateScope(); |
|
|
|
var contextName = typeof(TEfCoreDbContext).Name; |
|
|
|
//var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
|
|
|
|
//using var uow = uowManager.Begin();
|
|
|
|
var provider = scope.ServiceProvider.GetRequiredService<IDbContextProvider<TEfCoreDbContext>>(); |
|
|
|
//using var dbContext = provider.GetDbContextAsync().Result;
|
|
|
|
using var dbContext = scope.ServiceProvider.GetRequiredService(typeof(TEfCoreDbContext)) as DbContext; |
|
|
|
dbContext.Database.SetCommandTimeout(TimeSpan.FromMinutes(10)); |
|
|
|
var dbCreator = dbContext.GetService<IRelationalDatabaseCreator>() as RelationalDatabaseCreator; |
|
|
|
var sql = dbCreator.GenerateCreateScript(); |
|
|
|
sql = dbContext.Database.ProviderName.Contains("SqlServer") ? Regex.Replace(sql, ";\\s+GO\\s", " ") : sql; |
|
|
|
var md5 = sql.Md5(); |
|
|
|
var path = Path.Combine(Directory.GetCurrentDirectory(), "scripts"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
using var sw = File.CreateText(Path.Combine(path, $"db.{dbContext.Database.ProviderName}.{contextName}.sql")); |
|
|
|
sw.Write(sql); |
|
|
|
Console.WriteLine($"{contextName} 初始化开始"); |
|
|
|
Console.WriteLine($"ConnectionString:{dbContext.Database.GetConnectionString()}"); |
|
|
|
//创建数据库
|
|
|
|
if (!dbCreator.Exists()) |
|
|
|
{ |
|
|
|
dbCreator.Create(); |
|
|
|
var createSql = "CREATE TABLE EFDbContext(Id varchar(255) NOT NULL,Hash varchar(255) NOT NULL,PRIMARY KEY (Id));"; |
|
|
|
dbContext.Database.ExecuteSqlRaw(createSql); |
|
|
|
} |
|
|
|
// 查询当前DbContext是否已经初始化
|
|
|
|
using var conn = dbContext.Database.GetDbConnection(); |
|
|
|
var cmd = conn.CreateCommand(); |
|
|
|
conn.Open(); |
|
|
|
cmd.CommandText = $"SELECT Hash FROM EFDbContext where Id='{contextName}'"; |
|
|
|
var hash = cmd.ExecuteScalar(); |
|
|
|
//conn.Close();
|
|
|
|
//conn.Dispose();
|
|
|
|
if (hash == null) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
//dbContext.Database.BeginTransaction();
|
|
|
|
dbContext.Database.ExecuteSqlRaw(sql); |
|
|
|
dbContext.Database.ExecuteSqlRaw($"INSERT INTO EFDbContext VALUES ('{contextName}', '{md5}');"); |
|
|
|
context.ServiceProvider |
|
|
|
.GetRequiredService<IDataSeeder>() |
|
|
|
.SeedAsync() |
|
|
|
.Wait(); |
|
|
|
//dbContext.Database.CommitTransaction();
|
|
|
|
Console.WriteLine($"{contextName} 初始化成功"); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
//dbContext.Database.RollbackTransaction();
|
|
|
|
var message = $"{contextName} 初始化失败:{ex.Message}"; |
|
|
|
Console.WriteLine(message); |
|
|
|
Console.WriteLine(ex.ToString()); |
|
|
|
throw new Exception(message, ex); |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
Console.WriteLine($"{contextName} 初始化结束"); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
Console.WriteLine($"{contextName} 数据库结构{(hash.ToString() == md5 ? "正常" : "已过时")}"); |
|
|
|
} |
|
|
|
//uow.CompleteAsync();
|
|
|
|
//conn.Close();
|
|
|
|
//conn.Dispose();
|
|
|
|