Skip to content

Commit 5d27ef4

Browse files
committed
Reflection generates all models
反射生成全部实体
1 parent 45b8a40 commit 5d27ef4

18 files changed

Lines changed: 52 additions & 48 deletions

File tree

.docs/contents/Update/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

22
## 更新日志
33

4+
### 2020-06-08
5+
6+
> 简单项目更新:生成数据库表结构的时候,利用反射机制,自动生成固定命名空间 `Blog.Core.Model.Models` 下的全部实体.
7+
> 同时判断表是否存在,如果存在下次不再重复生成。
8+
9+
410
### 2020-06-06
511

612
项目更新:更新项目模板 `Update Blog.Core.Webapi.Template.2.1.0.nupkg` [1a726f8](https://github.com/anjoy8/Blog.Core/commit/1a726f890e527c978982071462e82db4478632f0),更新项目即可 。

Blog.Core.IRepository/IModuleRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Blog.Core.IRepository
66
/// <summary>
77
/// IModuleRepository
88
/// </summary>
9-
public interface IModuleRepository : IBaseRepository<Module>//类名
9+
public interface IModuleRepository : IBaseRepository<Modules>//类名
1010
{
1111

1212

Blog.Core.IServices/IModuleServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Blog.Core.IServices
66
/// <summary>
77
/// ModuleServices
88
/// </summary>
9-
public interface IModuleServices :IBaseServices<Module>
9+
public interface IModuleServices :IBaseServices<Modules>
1010
{
1111

1212

Blog.Core.Model/Models/Module.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Blog.Core.Model.Models
66
/// <summary>
77
/// 接口API地址信息表
88
/// </summary>
9-
public class Module : RootEntity
9+
public class Modules : RootEntity
1010
{
11-
public Module()
11+
public Modules()
1212
{
1313
//this.ChildModule = new List<Module>();
1414
//this.ModulePermission = new List<ModulePermission>();

Blog.Core.Model/Models/RoleModulePermission.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public RoleModulePermission()
6969
[SugarColumn(IsIgnore = true)]
7070
public Role Role { get; set; }
7171
[SugarColumn(IsIgnore = true)]
72-
public Module Module { get; set; }
72+
public Modules Module { get; set; }
7373
[SugarColumn(IsIgnore = true)]
7474
public Permission Permission { get; set; }
7575
}

Blog.Core.Model/Models/RootEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using SqlSugar;
22

3-
namespace Blog.Core.Model.Models
3+
namespace Blog.Core.Model
44
{
55
public class RootEntity
66
{

Blog.Core.Model/Seed/DBSeed.cs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using Blog.Core.Common;
22
using Blog.Core.Common.DB;
33
using Blog.Core.Common.Helper;
4+
using Blog.Core.Model.Models;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Linq;
9+
using System.Reflection;
810
using System.Text;
911
using 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

Blog.Core.Model/Seed/FrameSeed.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Collections.Generic;
44
using System.IO;
55

6-
namespace Blog.Core.Model.Models
6+
namespace Blog.Core.Model.Seed
77
{
88
public class FrameSeed
99
{

Blog.Core.Model/Seed/MyContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using SqlSugar;
33
using System;
44

5-
namespace Blog.Core.Model.Models
5+
namespace Blog.Core.Model.Seed
66
{
77
public class MyContext
88
{

Blog.Core.Repository/ModuleRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Blog.Core.Repository
88
/// <summary>
99
/// ModuleRepository
1010
/// </summary>
11-
public class ModuleRepository : BaseRepository<Module>, IModuleRepository
11+
public class ModuleRepository : BaseRepository<Modules>, IModuleRepository
1212
{
1313
public ModuleRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
1414
{

0 commit comments

Comments
 (0)