Skip to content

Commit

Permalink
Reflection generates all models
Browse files Browse the repository at this point in the history
反射生成全部实体
  • Loading branch information
anjoy8 committed Jun 8, 2020
1 parent 45b8a40 commit 5d27ef4
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 48 deletions.
6 changes: 6 additions & 0 deletions .docs/contents/Update/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

## 更新日志

### 2020-06-08

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

### 2020-06-06

项目更新:更新项目模板 `Update Blog.Core.Webapi.Template.2.1.0.nupkg` [1a726f8](https://github.com/anjoy8/Blog.Core/commit/1a726f890e527c978982071462e82db4478632f0),更新项目即可 。
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.IRepository/IModuleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Blog.Core.IRepository
/// <summary>
/// IModuleRepository
/// </summary>
public interface IModuleRepository : IBaseRepository<Module>//类名
public interface IModuleRepository : IBaseRepository<Modules>//类名
{


Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.IServices/IModuleServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Blog.Core.IServices
/// <summary>
/// ModuleServices
/// </summary>
public interface IModuleServices :IBaseServices<Module>
public interface IModuleServices :IBaseServices<Modules>
{


Expand Down
4 changes: 2 additions & 2 deletions Blog.Core.Model/Models/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Blog.Core.Model.Models
/// <summary>
/// 接口API地址信息表
/// </summary>
public class Module : RootEntity
public class Modules : RootEntity
{
public Module()
public Modules()
{
//this.ChildModule = new List<Module>();
//this.ModulePermission = new List<ModulePermission>();
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Model/Models/RoleModulePermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public RoleModulePermission()
[SugarColumn(IsIgnore = true)]
public Role Role { get; set; }
[SugarColumn(IsIgnore = true)]
public Module Module { get; set; }
public Modules Module { get; set; }
[SugarColumn(IsIgnore = true)]
public Permission Permission { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Model/Models/RootEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using SqlSugar;

namespace Blog.Core.Model.Models
namespace Blog.Core.Model
{
public class RootEntity
{
Expand Down
50 changes: 24 additions & 26 deletions Blog.Core.Model/Seed/DBSeed.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Blog.Core.Common;
using Blog.Core.Common.DB;
using Blog.Core.Common.Helper;
using Blog.Core.Model.Models;
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.Model.Models
namespace Blog.Core.Model.Seed
{
public class DBSeed
{
Expand Down Expand Up @@ -79,37 +81,33 @@ public static async Task SeedAsync(MyContext myContext, string WebRootPath)
}

Console.WriteLine();
Console.WriteLine($"Create Database(The Db Id:{MyContext.ConnId})...");


// 创建数据库
Console.WriteLine($"Create Database(The Db Id:{MyContext.ConnId})...");
myContext.Db.DbMaintenance.CreateDatabase();

ConsoleHelper.WriteSuccessLine($"Database created successfully!");

Console.WriteLine("Create Tables...");
// 创建表
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(TasksQz),
typeof(UserRole));

// 后期单独处理某些表
// myContext.Db.CodeFirst.InitTables(typeof(sysUserInfo));

// 创建数据库表,遍历指定命名空间下的class,
// 注意不要把其他命名空间下的也添加进来。
Console.WriteLine("Create Tables...");
var modelTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t.Namespace == "Blog.Core.Model.Models"
select t;
modelTypes.ToList().ForEach(t =>
{
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())
{
Console.WriteLine($"Seeding database data (The Db Id:{MyContext.ConnId})...");
Expand All @@ -128,9 +126,9 @@ public static async Task SeedAsync(MyContext myContext, string WebRootPath)


#region Module
if (!await myContext.Db.Queryable<Module>().AnyAsync())
if (!await myContext.Db.Queryable<Modules>().AnyAsync())
{
myContext.GetEntityDB<Module>().InsertRange(JsonHelper.ParseFormByJson<List<Module>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "Module"), Encoding.UTF8)));
myContext.GetEntityDB<Modules>().InsertRange(JsonHelper.ParseFormByJson<List<Modules>>(FileHelper.ReadFile(string.Format(SeedDataFolder, "Module"), Encoding.UTF8)));
Console.WriteLine("Table:Module created success!");
}
else
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Model/Seed/FrameSeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.IO;

namespace Blog.Core.Model.Models
namespace Blog.Core.Model.Seed
{
public class FrameSeed
{
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Model/Seed/MyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using SqlSugar;
using System;

namespace Blog.Core.Model.Models
namespace Blog.Core.Model.Seed
{
public class MyContext
{
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Repository/ModuleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Blog.Core.Repository
/// <summary>
/// ModuleRepository
/// </summary>
public class ModuleRepository : BaseRepository<Module>, IModuleRepository
public class ModuleRepository : BaseRepository<Modules>, IModuleRepository
{
public ModuleRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
{
Expand Down
4 changes: 2 additions & 2 deletions Blog.Core.Repository/RoleModulePermissionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<List<RoleModulePermission>> WithChildrenModel()

public async Task<List<TestMuchTableResult>> QueryMuchTable()
{
return await QueryMuch<RoleModulePermission, Module, Permission, TestMuchTableResult>(
return await QueryMuch<RoleModulePermission, Modules, Permission, TestMuchTableResult>(
(rmp, m, p) => new object[] {
JoinType.Left, rmp.ModuleId == m.Id,
JoinType.Left, rmp.PermissionId == p.Id
Expand All @@ -56,7 +56,7 @@ public async Task<List<TestMuchTableResult>> QueryMuchTable()
/// <returns></returns>
public async Task<List<RoleModulePermission>> RoleModuleMaps()
{
return await QueryMuch<RoleModulePermission, Module, Role, RoleModulePermission>(
return await QueryMuch<RoleModulePermission, Modules, Role, RoleModulePermission>(
(rmp, m, r) => new object[] {
JoinType.Left, rmp.ModuleId == m.Id,
JoinType.Left, rmp.RoleId == r.Id
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Services/ModuleServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Blog.Core.Services
/// <summary>
/// ModuleServices
/// </summary>
public class ModuleServices : BaseServices<Module>, IModuleServices
public class ModuleServices : BaseServices<Modules>, IModuleServices
{

IModuleRepository _dal;
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core/Controllers/DbFirst/DbFirstController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Blog.Core.Common.DB;
using Blog.Core.Model;
using Blog.Core.Model.Models;
using Blog.Core.Model.Seed;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
Expand Down
10 changes: 5 additions & 5 deletions Blog.Core/Controllers/ModuleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public ModuleController(IModuleServices moduleServices, IUser user)
/// <returns></returns>
// GET: api/User
[HttpGet]
public async Task<MessageModel<PageModel<Module>>> Get(int page = 1, string key = "")
public async Task<MessageModel<PageModel<Modules>>> Get(int page = 1, string key = "")
{
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = "";
}
int intPageSize = 50;

Expression<Func<Module, bool>> whereExpression = a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key));
Expression<Func<Modules, bool>> whereExpression = a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key));

var data = await _moduleServices.QueryPage(whereExpression, page, intPageSize, " Id desc ");

return new MessageModel<PageModel<Module>>()
return new MessageModel<PageModel<Modules>>()
{
msg = "获取成功",
success = data.dataCount >= 0,
Expand All @@ -71,7 +71,7 @@ public string Get(string id)
/// <returns></returns>
// POST: api/User
[HttpPost]
public async Task<MessageModel<string>> Post([FromBody] Module module)
public async Task<MessageModel<string>> Post([FromBody] Modules module)
{
var data = new MessageModel<string>();

Expand All @@ -96,7 +96,7 @@ public async Task<MessageModel<string>> Post([FromBody] Module module)
/// <returns></returns>
// PUT: api/User/5
[HttpPut]
public async Task<MessageModel<string>> Put([FromBody] Module module)
public async Task<MessageModel<string>> Put([FromBody] Modules module)
{
var data = new MessageModel<string>();
if (module != null && module.Id > 0)
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core/Extensions/DbSetup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Blog.Core.Model.Models;
using Blog.Core.Model.Seed;
using Microsoft.Extensions.DependencyInjection;
using System;

Expand Down
2 changes: 1 addition & 1 deletion Blog.Core/Middlewares/SeedDataMildd.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Blog.Core.Common;
using Blog.Core.Model.Models;
using Blog.Core.Model.Seed;
using log4net;
using Microsoft.AspNetCore.Builder;
using System;
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core/Middlewares/SeedDbDataMildd.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Blog.Core.Common;
using Blog.Core.Model.Models;
using Blog.Core.Model.Seed;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Blog.Core.Hubs;
using Blog.Core.IServices;
using Blog.Core.Middlewares;
using Blog.Core.Model.Models;
using Blog.Core.Model.Seed;
using Blog.Core.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down

0 comments on commit 5d27ef4

Please sign in to comment.