Skip to content

Commit 55ae4fb

Browse files
committed
add alert when seeding data
在项目启动的时候,提示数据库信息
1 parent 95887fb commit 55ae4fb

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

Blog.Core.Model/Seed/DBSeed.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Blog.Core.Common;
2+
using Blog.Core.Common.DB;
23
using Blog.Core.Common.Helper;
34
using System;
45
using System.Collections.Generic;
56
using System.IO;
7+
using System.Linq;
68
using System.Text;
79
using System.Threading.Tasks;
810

@@ -41,12 +43,34 @@ public static async Task SeedAsync(MyContext myContext, string WebRootPath)
4143
SeedDataFolderMini = Path.Combine(WebRootPath, SeedDataFolderMini);
4244

4345
Console.WriteLine("Config data init...");
44-
Console.WriteLine("DB Type: " + MyContext.DbType);
45-
Console.WriteLine("DB ConnectString: " + MyContext.ConnectionString);
46+
Console.WriteLine($"Is multi-DataBase: {Appsettings.app(new string[] { "MutiDBEnabled" })}");
47+
if (Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool())
48+
{
49+
Console.WriteLine($"Master DB Type: {MyContext.DbType}");
50+
Console.WriteLine($"Master DB ConnectString: {MyContext.ConnectionString}");
51+
Console.WriteLine();
52+
53+
var slaveIndex = 0;
54+
BaseDBConfig.MutiConnectionString.Where(x => x.ConnId != MainDb.CurrentDbConnId).ToList().ForEach(m =>
55+
{
56+
slaveIndex++;
57+
Console.WriteLine($"Slave{slaveIndex} DB ID: {m.ConnId}");
58+
Console.WriteLine($"Slave{slaveIndex} DB Type: {m.DbType}");
59+
Console.WriteLine($"Slave{slaveIndex} DB ConnectString: {m.Conn}");
60+
});
61+
62+
}
63+
else
64+
{
65+
Console.WriteLine("DB Type: " + MyContext.DbType);
66+
Console.WriteLine("DB ConnectString: " + MyContext.ConnectionString);
67+
}
4668

69+
Console.WriteLine("Create Database...");
4770
// 创建数据库
4871
myContext.Db.DbMaintenance.CreateDatabase();
4972

73+
Console.WriteLine("Create Tables...");
5074
// 创建表
5175
myContext.CreateTableByEntity(false,
5276
typeof(Advertisement),
@@ -67,7 +91,7 @@ public static async Task SeedAsync(MyContext myContext, string WebRootPath)
6791
// 后期单独处理某些表
6892
// myContext.Db.CodeFirst.InitTables(typeof(sysUserInfo));
6993

70-
Console.WriteLine("Database:WMBlog created success!");
94+
Console.WriteLine("Database is created success!");
7195
Console.WriteLine();
7296

7397
if (Appsettings.app(new string[] { "AppSettings", "SeedDBDataEnabled" }).ObjToBool())

Blog.Core.Repository/BASE/BaseRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private ISqlSugarClient _db
3131
*/
3232
if (Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool())
3333
{
34-
if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable)
34+
if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription))
3535
{
3636
_dbBase.ChangeDatabase(sugarTable.TableDescription.ToLower());
3737
}

0 commit comments

Comments
 (0)