forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBSeed.cs
More file actions
325 lines (271 loc) · 14.6 KB
/
DBSeed.cs
File metadata and controls
325 lines (271 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using Blog.Core.Common.DB;
using Blog.Core.Common.Helper;
using Blog.Core.Model.Models;
using Magicodes.ExporterAndImporter.Excel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Blog.Core.Common.Seed
{
public class DBSeed
{
private static string SeedDataFolder = "BlogCore.Data.json/{0}.tsv";
/// <summary>
/// 异步添加种子数据
/// </summary>
/// <param name="myContext"></param>
/// <param name="WebRootPath"></param>
/// <returns></returns>
public static async Task SeedAsync(MyContext myContext, string WebRootPath)
{
try
{
if (string.IsNullOrEmpty(WebRootPath))
{
throw new Exception("获取wwwroot路径时,异常!");
}
SeedDataFolder = Path.Combine(WebRootPath, SeedDataFolder);
Console.WriteLine("************ Blog.Core DataBase Set *****************");
Console.WriteLine($"Is multi-DataBase: {AppSettings.app(new string[] { "MutiDBEnabled" })}");
Console.WriteLine($"Is CQRS: {AppSettings.app(new string[] { "CQRSEnabled" })}");
Console.WriteLine();
Console.WriteLine($"Master DB ConId: {MyContext.ConnId}");
Console.WriteLine($"Master DB Type: {MyContext.DbType}");
Console.WriteLine($"Master DB ConnectString: {MyContext.ConnectionString}");
Console.WriteLine();
if (AppSettings.app(new string[] { "MutiDBEnabled" }).ObjToBool())
{
var slaveIndex = 0;
BaseDBConfig.MutiConnectionString.allDbs.Where(x => x.ConnId != MainDb.CurrentDbConnId).ToList().ForEach(m =>
{
slaveIndex++;
Console.WriteLine($"Slave{slaveIndex} DB ID: {m.ConnId}");
Console.WriteLine($"Slave{slaveIndex} DB Type: {m.DbType}");
Console.WriteLine($"Slave{slaveIndex} DB ConnectString: {m.Connection}");
Console.WriteLine($"--------------------------------------");
});
}
else if (AppSettings.app(new string[] { "CQRSEnabled" }).ObjToBool())
{
var slaveIndex = 0;
BaseDBConfig.MutiConnectionString.slaveDbs.Where(x => x.ConnId != MainDb.CurrentDbConnId).ToList().ForEach(m =>
{
slaveIndex++;
Console.WriteLine($"Slave{slaveIndex} DB ID: {m.ConnId}");
Console.WriteLine($"Slave{slaveIndex} DB Type: {m.DbType}");
Console.WriteLine($"Slave{slaveIndex} DB ConnectString: {m.Connection}");
Console.WriteLine($"--------------------------------------");
});
}
else
{
}
Console.WriteLine();
// 创建数据库
Console.WriteLine($"Create Database(The Db Id:{MyContext.ConnId})...");
if (MyContext.DbType != SqlSugar.DbType.Oracle)
{
myContext.Db.DbMaintenance.CreateDatabase();
ConsoleHelper.WriteSuccessLine($"Database created successfully!");
}
else
{
//Oracle 数据库不支持该操作
ConsoleHelper.WriteSuccessLine($"Oracle 数据库不支持该操作,可手动创建Oracle数据库!");
}
// 创建数据库表,遍历指定命名空间下的class,
// 注意不要把其他命名空间下的也添加进来。
Console.WriteLine("Create Tables...");
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = System.IO.Directory.GetFiles(path, "Blog.Core.Model.dll").Select(Assembly.LoadFrom).ToArray();
var modelTypes = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(x => x.IsClass && x.Namespace != null && x.Namespace.Equals("Blog.Core.Model.Models")).ToList();
modelTypes.ForEach(t =>
{
// 这里只支持添加表,不支持删除
// 如果想要删除,数据库直接右键删除,或者联系SqlSugar作者;
if (!myContext.Db.DbMaintenance.IsAnyTable(t.Name))
{
Console.WriteLine(t.Name);
myContext.Db.CodeFirst.InitTables(t);
}
});
ConsoleHelper.WriteSuccessLine($"Tables created successfully!");
Console.WriteLine();
if (AppSettings.app(new string[] { "AppSettings", "SeedDBDataEnabled" }).ObjToBool())
{
JsonSerializerSettings setting = new JsonSerializerSettings();
JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
{
//日期类型默认格式化处理
setting.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//空值处理
setting.NullValueHandling = NullValueHandling.Ignore;
//高级用法九中的Bool类型转换 设置
//setting.Converters.Add(new BoolConvert("是,否"));
return setting;
});
Console.WriteLine($"Seeding database data (The Db Id:{MyContext.ConnId})...");
var importer = new ExcelImporter();
#region BlogArticle
if (!await myContext.Db.Queryable<BlogArticle>().AnyAsync())
{
myContext.GetEntityDB<BlogArticle>().InsertRange(JsonHelper.ParseFormByJson<List<BlogArticle>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "BlogArticle"), Encoding.UTF8)));
Console.WriteLine("Table:BlogArticle created success!");
}
else
{
Console.WriteLine("Table:BlogArticle already exists...");
}
#endregion
#region Modules
if (!await myContext.Db.Queryable<Modules>().AnyAsync())
{
var data = JsonConvert.DeserializeObject<List<Modules>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "Modules"), Encoding.UTF8), setting);
myContext.GetEntityDB<Modules>().InsertRange(data);
Console.WriteLine("Table:Modules created success!");
}
else
{
Console.WriteLine("Table:Modules already exists...");
}
#endregion
#region Permission
if (!await myContext.Db.Queryable<Permission>().AnyAsync())
{
var data = JsonConvert.DeserializeObject<List<Permission>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "Permission"), Encoding.UTF8), setting);
myContext.GetEntityDB<Permission>().InsertRange(data);
Console.WriteLine("Table:Permission created success!");
}
else
{
Console.WriteLine("Table:Permission already exists...");
}
#endregion
#region Role
if (!await myContext.Db.Queryable<Role>().AnyAsync())
{
//var data = JsonConvert.DeserializeObject<List<Role>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "Role"), Encoding.UTF8), setting);
using var stream = new FileStream(Path.Combine(WebRootPath, "BlogCore.Data.excel", "Role.xlsx"), FileMode.Open);
var result = await importer.Import<Role>(stream);
var data = result.Data.ToList();
myContext.GetEntityDB<Role>().InsertRange(data);
Console.WriteLine("Table:Role created success!");
}
else
{
Console.WriteLine("Table:Role already exists...");
}
#endregion
#region RoleModulePermission
if (!await myContext.Db.Queryable<RoleModulePermission>().AnyAsync())
{
var data = JsonConvert.DeserializeObject<List<RoleModulePermission>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "RoleModulePermission"), Encoding.UTF8), setting);
myContext.GetEntityDB<RoleModulePermission>().InsertRange(data);
Console.WriteLine("Table:RoleModulePermission created success!");
}
else
{
Console.WriteLine("Table:RoleModulePermission already exists...");
}
#endregion
#region Topic
if (!await myContext.Db.Queryable<Topic>().AnyAsync())
{
var data = JsonConvert.DeserializeObject<List<Topic>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "Topic"), Encoding.UTF8), setting);
myContext.GetEntityDB<Topic>().InsertRange(data);
Console.WriteLine("Table:Topic created success!");
}
else
{
Console.WriteLine("Table:Topic already exists...");
}
#endregion
#region TopicDetail
if (!await myContext.Db.Queryable<TopicDetail>().AnyAsync())
{
var data = JsonConvert.DeserializeObject<List<TopicDetail>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "TopicDetail"), Encoding.UTF8), setting);
myContext.GetEntityDB<TopicDetail>().InsertRange(data);
Console.WriteLine("Table:TopicDetail created success!");
}
else
{
Console.WriteLine("Table:TopicDetail already exists...");
}
#endregion
#region UserRole
if (!await myContext.Db.Queryable<UserRole>().AnyAsync())
{
//var data = JsonConvert.DeserializeObject<List<UserRole>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "UserRole"), Encoding.UTF8), setting);
using var stream = new FileStream(Path.Combine(WebRootPath, "BlogCore.Data.excel", "UserRole.xlsx"), FileMode.Open);
var result = await importer.Import<UserRole>(stream);
var data = result.Data.ToList();
myContext.GetEntityDB<UserRole>().InsertRange(data);
Console.WriteLine("Table:UserRole created success!");
}
else
{
Console.WriteLine("Table:UserRole already exists...");
}
#endregion
#region sysUserInfo
if (!await myContext.Db.Queryable<SysUserInfo>().AnyAsync())
{
//var data = JsonConvert.DeserializeObject<List<SysUserInfo>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "sysUserInfo"), Encoding.UTF8), setting);
using var stream = new FileStream(Path.Combine(WebRootPath, "BlogCore.Data.excel", "SysUserInfo.xlsx"), FileMode.Open);
var result = await importer.Import<SysUserInfo>(stream);
var data = result.Data.ToList();
myContext.GetEntityDB<SysUserInfo>().InsertRange(data);
Console.WriteLine("Table:sysUserInfo created success!");
}
else
{
Console.WriteLine("Table:sysUserInfo already exists...");
}
#endregion
#region TasksQz
if (!await myContext.Db.Queryable<TasksQz>().AnyAsync())
{
var data = JsonConvert.DeserializeObject<List<TasksQz>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "TasksQz"), Encoding.UTF8), setting);
myContext.GetEntityDB<TasksQz>().InsertRange(data);
Console.WriteLine("Table:TasksQz created success!");
}
else
{
Console.WriteLine("Table:TasksQz already exists...");
}
#endregion
#region Department
if (!await myContext.Db.Queryable<Department>().AnyAsync())
{
var data = JsonConvert.DeserializeObject<List<Department>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "Department"), Encoding.UTF8), setting);
myContext.GetEntityDB<Department>().InsertRange(data);
Console.WriteLine("Table:Department created success!");
}
else
{
Console.WriteLine("Table:Department already exists...");
}
#endregion
ConsoleHelper.WriteSuccessLine($"Done seeding database!");
}
Console.WriteLine();
}
catch (Exception ex)
{
throw new Exception(
$"1、若是Mysql,查看常见问题:https://github.com/anjoy8/Blog.Core/issues/148#issue-776281770 \n" +
$"2、若是Oracle,查看常见问题:https://github.com/anjoy8/Blog.Core/issues/148#issuecomment-752340231 \n" +
"3、其他错误:" + ex.Message);
}
}
}
}