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
178 lines (154 loc) · 7.06 KB
/
DBSeed.cs
File metadata and controls
178 lines (154 loc) · 7.06 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
using Blog.Core.Common.Helper;
using Blog.Core.Model.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Blog.Core.Model.Models
{
public class DBSeed
{
private static string GitJsonFileFormat = "https://github.com/anjoy8/Blog.Data.Share/raw/master/Blog.Core.Data.json/{0}.tsv";
/// <summary>
/// 异步添加种子数据
/// </summary>
/// <param name="myContext"></param>
/// <returns></returns>
public static async Task SeedAsync(MyContext myContext)
{
try
{
// 注意!一定要先手动创建一个【空的数据库】
// 如果生成过了,第二次,就不用再执行一遍了,注释掉该方法即可
myContext.CreateTableByEntity(false,
typeof(Advertisement),
typeof(BlogArticle),
typeof(Guestbook),
typeof(Module),
typeof(ModulePermission),
typeof(OperateLog),
typeof(PasswordLib),
typeof(Permission),
typeof(Role),
typeof(RoleModulePermission),
typeof(sysUserInfo),
typeof(Topic),
typeof(TopicDetail),
typeof(UserRole));
// 后期单独处理某些表
//myContext.Db.CodeFirst.InitTables(typeof(sysUserInfo));
//myContext.Db.CodeFirst.InitTables(typeof(Permission));
//myContext.Db.CodeFirst.InitTables(typeof(Advertisement));
Console.WriteLine("Database:WMBlog created success!");
Console.WriteLine();
Console.WriteLine("Seeding database...");
#region BlogArticle
if (!await myContext.Db.Queryable<BlogArticle>().AnyAsync())
{
myContext.GetEntityDB<BlogArticle>().InsertRange(JsonHelper.ParseFormByJson<List<BlogArticle>>(GetNetData.Get(string.Format(GitJsonFileFormat, "BlogArticle"))));
Console.WriteLine("Table:BlogArticle created success!");
}
else
{
Console.WriteLine("Table:BlogArticle already exists...");
}
#endregion
#region Module
if (!await myContext.Db.Queryable<Module>().AnyAsync())
{
myContext.GetEntityDB<Module>().InsertRange(JsonHelper.ParseFormByJson<List<Module>>(GetNetData.Get(string.Format(GitJsonFileFormat, "Module"))));
Console.WriteLine("Table:Module created success!");
}
else
{
Console.WriteLine("Table:Module already exists...");
}
#endregion
#region Permission
if (!await myContext.Db.Queryable<Permission>().AnyAsync())
{
myContext.GetEntityDB<Permission>().InsertRange(JsonHelper.ParseFormByJson<List<Permission>>(GetNetData.Get(string.Format(GitJsonFileFormat, "Permission"))));
Console.WriteLine("Table:Permission created success!");
}
else
{
Console.WriteLine("Table:Permission already exists...");
}
#endregion
#region Role
if (!await myContext.Db.Queryable<Role>().AnyAsync())
{
myContext.GetEntityDB<Role>().InsertRange(JsonHelper.ParseFormByJson<List<Role>>(GetNetData.Get(string.Format(GitJsonFileFormat, "Role"))));
Console.WriteLine("Table:Role created success!");
}
else
{
Console.WriteLine("Table:Role already exists...");
}
#endregion
#region RoleModulePermission
if (!await myContext.Db.Queryable<RoleModulePermission>().AnyAsync())
{
myContext.GetEntityDB<RoleModulePermission>().InsertRange(JsonHelper.ParseFormByJson<List<RoleModulePermission>>(GetNetData.Get(string.Format(GitJsonFileFormat, "RoleModulePermission"))));
Console.WriteLine("Table:RoleModulePermission created success!");
}
else
{
Console.WriteLine("Table:RoleModulePermission already exists...");
}
#endregion
#region Topic
if (!await myContext.Db.Queryable<Topic>().AnyAsync())
{
myContext.GetEntityDB<Topic>().InsertRange(JsonHelper.ParseFormByJson<List<Topic>>(GetNetData.Get(string.Format(GitJsonFileFormat, "Topic"))));
Console.WriteLine("Table:Topic created success!");
}
else
{
Console.WriteLine("Table:Topic already exists...");
}
#endregion
#region TopicDetail
if (!await myContext.Db.Queryable<TopicDetail>().AnyAsync())
{
myContext.GetEntityDB<TopicDetail>().InsertRange(JsonHelper.ParseFormByJson<List<TopicDetail>>(GetNetData.Get(string.Format(GitJsonFileFormat, "TopicDetail"))));
Console.WriteLine("Table:TopicDetail created success!");
}
else
{
Console.WriteLine("Table:TopicDetail already exists...");
}
#endregion
#region UserRole
if (!await myContext.Db.Queryable<UserRole>().AnyAsync())
{
myContext.GetEntityDB<UserRole>().InsertRange(JsonHelper.ParseFormByJson<List<UserRole>>(GetNetData.Get(string.Format(GitJsonFileFormat, "UserRole"))));
Console.WriteLine("Table:UserRole created success!");
}
else
{
Console.WriteLine("Table:UserRole already exists...");
}
#endregion
#region sysUserInfo
if (!await myContext.Db.Queryable<sysUserInfo>().AnyAsync())
{
myContext.GetEntityDB<sysUserInfo>().InsertRange(JsonHelper.ParseFormByJson<List<sysUserInfo>>(GetNetData.Get(string.Format(GitJsonFileFormat, "sysUserInfo"))));
Console.WriteLine("Table:sysUserInfo created success!");
}
else
{
Console.WriteLine("Table:sysUserInfo already exists...");
}
#endregion
Console.WriteLine("Done seeding database.");
Console.WriteLine();
}
catch (Exception ex)
{
throw new Exception("1、注意要先创建空的数据库\n2、" + ex.Message);
}
}
}
}