11using Blog . Core . Common ;
22using Blog . Core . Common . DB ;
33using Blog . Core . Common . Helper ;
4+ using Blog . Core . Model . Models ;
45using System ;
56using System . Collections . Generic ;
67using System . IO ;
78using System . Linq ;
9+ using System . Reflection ;
810using System . Text ;
911using System . Threading . Tasks ;
1012
11- namespace Blog . Core . Model . Models
13+ namespace Blog . Core . Model . Seed
1214{
1315 public class DBSeed
1416 {
@@ -79,37 +81,33 @@ public static async Task SeedAsync(MyContext myContext, string WebRootPath)
7981 }
8082
8183 Console . WriteLine ( ) ;
82- Console . WriteLine ( $ "Create Database(The Db Id:{ MyContext . ConnId } )...") ;
84+
85+
8386 // 创建数据库
87+ Console . WriteLine ( $ "Create Database(The Db Id:{ MyContext . ConnId } )...") ;
8488 myContext . Db . DbMaintenance . CreateDatabase ( ) ;
85-
8689 ConsoleHelper . WriteSuccessLine ( $ "Database created successfully!") ;
8790
88- Console . WriteLine ( "Create Tables..." ) ;
89- // 创建表
90- myContext . CreateTableByEntity ( false ,
91- typeof ( Advertisement ) ,
92- typeof ( BlogArticle ) ,
93- typeof ( Guestbook ) ,
94- typeof ( Module ) ,
95- typeof ( ModulePermission ) ,
96- typeof ( OperateLog ) ,
97- typeof ( PasswordLib ) ,
98- typeof ( Permission ) ,
99- typeof ( Role ) ,
100- typeof ( RoleModulePermission ) ,
101- typeof ( sysUserInfo ) ,
102- typeof ( Topic ) ,
103- typeof ( TopicDetail ) ,
104- typeof ( TasksQz ) ,
105- typeof ( UserRole ) ) ;
106-
107- // 后期单独处理某些表
108- // myContext.Db.CodeFirst.InitTables(typeof(sysUserInfo));
10991
92+ // 创建数据库表,遍历指定命名空间下的class,
93+ // 注意不要把其他命名空间下的也添加进来。
94+ Console . WriteLine ( "Create Tables..." ) ;
95+ var modelTypes = from t in Assembly . GetExecutingAssembly ( ) . GetTypes ( )
96+ where t . IsClass && t . Namespace == "Blog.Core.Model.Models"
97+ select t ;
98+ modelTypes . ToList ( ) . ForEach ( t =>
99+ {
100+ if ( ! myContext . Db . DbMaintenance . IsAnyTable ( t . Name ) )
101+ {
102+ Console . WriteLine ( t . Name ) ;
103+ myContext . Db . CodeFirst . InitTables ( t ) ;
104+ }
105+ } ) ;
110106 ConsoleHelper . WriteSuccessLine ( $ "Tables created successfully!") ;
111107 Console . WriteLine ( ) ;
112108
109+
110+
113111 if ( Appsettings . app ( new string [ ] { "AppSettings" , "SeedDBDataEnabled" } ) . ObjToBool ( ) )
114112 {
115113 Console . WriteLine ( $ "Seeding database data (The Db Id:{ MyContext . ConnId } )...") ;
@@ -128,9 +126,9 @@ public static async Task SeedAsync(MyContext myContext, string WebRootPath)
128126
129127
130128 #region Module
131- if ( ! await myContext . Db . Queryable < Module > ( ) . AnyAsync ( ) )
129+ if ( ! await myContext . Db . Queryable < Modules > ( ) . AnyAsync ( ) )
132130 {
133- myContext . GetEntityDB < Module > ( ) . InsertRange ( JsonHelper . ParseFormByJson < List < Module > > ( FileHelper . ReadFile ( string . Format ( SeedDataFolder , "Module" ) , Encoding . UTF8 ) ) ) ;
131+ myContext . GetEntityDB < Modules > ( ) . InsertRange ( JsonHelper . ParseFormByJson < List < Modules > > ( FileHelper . ReadFile ( string . Format ( SeedDataFolder , "Module" ) , Encoding . UTF8 ) ) ) ;
134132 Console . WriteLine ( "Table:Module created success!" ) ;
135133 }
136134 else
0 commit comments