系列目錄
三. 集成輕量級ORM框架——SqlSugar
本章欲利用SqlSugar自帶DbFirst特性,實現將數據庫表直接生成到項目的實體類中。
系統會根據數據庫的某張表自動生成一個.cs文件,並把這個文件放到我們項目的實體類層RayPI.Entity中。
1)數據庫建表
在數據中新建一張用於測試的表——Book,字段有:Tid(Id),Title(書名),Writer(作者)。其中Tid為主鍵,並設置了自增長。並且每個字段都添加了相應的注釋文字。
2)RayPI.IService 數據接口層
添加IEntity接口類,該接口類與之前的接口類不太一樣,它不用基礎的CRUD函數,只有需要一個CreateEntity函數,用於生成實體類。

namespace RayPI.IService { /// <summary>
/// 實體數據接口 /// </summary>
public interface IEntity { /// <summary>
/// 生成實體類 /// </summary>
/// <param name="entityName"></param>
/// <param name="filePath"></param>
/// <returns></returns>
bool CreateEntity(string entityName,string filePath); } }
3)RayPI.Service 數據接口層
該數據層需要使用SqlSugarClient,SimpleClient已經滿足不了我們的需求了。

using RayPI.IService; using RayPI.Model; using SqlSugar; using System; namespace RayPI.Service { /// <summary>
/// 實體操作服務 /// </summary>
public class EntityService : BaseDB, IEntity { public SqlSugarClient db = GetClient(); /// <summary>
/// 生成實體類 /// </summary>
/// <param name="entityName"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public bool CreateEntity(string entityName,string filePath) { try { db.DbFirst.IsCreateAttribute().Where(entityName).CreateClassFile(filePath); return true; } catch (Exception) { return false; } } } }
其中entityName為表名,filePath為該.cs文件存儲的位置。
4)RayPI 控制器層
新建一個控制器Entity,這里,有個額外的工作,就是我們要獲取到項目的實際路徑,方法是利用IHostingEnvironment

using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using RayPI.Bussiness.Admin; namespace RayPI.Controllers.Admin { /// <summary>
/// 實體操作模塊 /// </summary>
[Produces("application/json")] [Route("api/[controller]")] public class EntityController : Controller { private EntityBLL bll = new EntityBLL(); private readonly IHostingEnvironment _hostingEnvironment; /// <summary>
/// 構造函數 /// </summary>
/// <param name="hostingEnvironment"></param>
public EntityController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } /// <summary>
/// 生成實體類 /// </summary>
/// <param name="entityName"></param>
/// <returns></returns>
[HttpPost] public JsonResult CreateEntity(string entityName=null) { if (entityName == null) return Json("參數為空"); return Json(bll.CreateEntity(entityName,_hostingEnvironment.ContentRootPath)); } } }
這里的_hostingEnvironment.ContentRootPath獲取到的是主項目目錄,例如“D:\\MyProjects\RayPI/RayPI”,我們要將實體類存入的地址應該是“D:\\MyProjects\RayPI/RayPI.Entity”下面,所以接下來還需要對這個地址進行相應操作。
5)RayPI.Bussiness 業務邏輯層

using RayPI.IService; using RayPI.Model; using RayPI.Service; namespace RayPI.Bussiness.Admin { public class EntityBLL { private IEntity iService = new EntityService(); public MessageModel<string> CreateEntity(string entityName,string contentRootPath) { string[] arr = contentRootPath.Split('\\'); string baseFileProvider = ""; for (int i = 0; i < arr.Length-1; i++) { baseFileProvider += arr[i]; baseFileProvider += "\\"; } string filePath = baseFileProvider + "RayPI.Entity"; if (iService.CreateEntity(entityName, filePath)) return new MessageModel<string> { Success = true, Msg = "生成成功" }; else
return new MessageModel<string> { Success = false, Msg = "生成失敗" }; } } }
這里拿到的contentRootPath為主項目地址,所以我做了一點運算。
總感覺.NET Core相比於.NET Framwork獲取項目路徑要費事很多,關於獲取的各種方法,后面會再專門拿出一章來講。如果發現有比目前這種更方便的實現方法的話,我也會及時更新的。
下面F5運行調試,在swagger中進行測試。
輸入表名(Book),點擊Excute,返回“生成成功”
回到項目里,打開RayPi.Entity,發現比之前多了一個實體類——Book.cs
並且也同時生成了我們為字段編寫的注釋和字段的本身屬性(默認值、主鍵、自增長等)
到這,自動生成實體類的功能就算實現了,
后面我們向項目集成layui前端框架后,會將該功能集成到后台管理系統里,從而實現在后台通過填寫數據表名+點擊按鈕就可以生成實體類的功能