Series Catalogue
A . Create projects and integrate swagger
1.1 Create
1.2 Perfect
Two. Building the overall structure of the project
Three. Integrated Lightweight ORM Framework--sqlsugar
3.1 Building the environment
3.2 Actual combat: Quickly implement CRUD with Sqlsugar
3.3 Generating Entity classes automatically
This chapter is intended to take advantage of Sqlsugar's own Dbfirst feature, which enables you to generate database tables directly into the entity classes of your project.
The system automatically generates a. cs file based on a table in the database and puts the file into the entity class layer raypi.entity of our project.
1) Database Build table
Create a new table--book for testing in the data, with the following fields: Tid (Id), title (title), writer (author). Where the TID is the primary key and the self-growth is set. and the corresponding comment text is added to each field.
2) Raypi.iservice Data Interface layer
Add the IEntity interface class, which is not the same as the previous interface class, it does not use the underlying CRUD functions, only requires a createentity function to generate the entity class.
namespaceraypi.iservice{/// <summary> ///Entity Data Interface/// </summary> Public Interfaceientity {/// <summary> ///Generating entity Classes/// </summary> /// <param name= "EntityName" ></param> /// <param name= "FilePath" ></param> /// <returns></returns> BOOLCreateentity (stringEntityName,stringFilePath); }}
ientity
3) Raypi.service Data Interface layer
The data tier needs to use Sqlsugarclient,simpleclient to meet our needs.
usingRaypi.iservice;usingRaypi.model;usingSqlsugar;usingSystem;namespaceraypi.service{/// <summary> ///Entity Operations Services/// </summary> Public classEntityservice:basedb, ientity { PublicSqlsugarclient db =getclient (); /// <summary> ///Generating entity Classes/// </summary> /// <param name= "EntityName" ></param> /// <param name= "FilePath" ></param> /// <returns></returns> Public BOOLCreateentity (stringEntityName,stringFilePath) { Try{db. Dbfirst.iscreateattribute (). Where (EntityName). Createclassfile (FilePath); return true; } Catch(Exception) {return false; } } }}
Entityservice
Where EntityName is the table name and filepath is the location where the. cs file is stored.
4) Raypi Controller layer
Create a new controller entity, here, an extra job is to get the actual path to the project, by using the Ihostingenvironment
usingMicrosoft.AspNetCore.Hosting;usingMICROSOFT.ASPNETCORE.MVC;usingRayPI.Bussiness.Admin;namespaceraypi.controllers.admin{/// <summary> ///Entity Operation Module/// </summary>[Produces ("Application/json")] [Route ("Api/[controller]")] Public classEntitycontroller:controller {PrivateENTITYBLL BLL =NewENTITYBLL (); Private ReadOnlyihostingenvironment _hostingenvironment; /// <summary> ///constructor Function/// </summary> /// <param name= "hostingenvironment" ></param> PublicEntitycontroller (ihostingenvironment hostingenvironment) {_hostingenvironment=hostingenvironment; } /// <summary> ///Generating entity Classes/// </summary> /// <param name= "EntityName" ></param> /// <returns></returns>[HttpPost] PublicJsonresult createentity (stringEntityname=NULL) { if(EntityName = =NULL) returnJson ("parameter is empty"); returnJson (BLL. Createentity (Entityname,_hostingenvironment.contentrootpath)); } }}
Entitycontroller
Here _hostingenvironment.contentrootpath get the main project directory, such as "D:\\myprojects\raypi/raypi", we want to deposit the entity class address should be "d:\\myprojects \raypi/raypi.entity "below, so you will need to work on this address accordingly.
5) raypi.bussiness Business Logic Layer
usingRaypi.iservice;usingRaypi.model;usingRaypi.service;namespaceraypi.bussiness.admin{ Public classENTITYBLL {PrivateIEntity IService =NewEntityservice (); Publicmessagemodel<string> createentity (stringEntityName,stringContentrootpath) { string[] arr = Contentrootpath.split ('\\'); stringBasefileprovider =""; for(inti =0; I < arr. length-1; i++) {Basefileprovider+=Arr[i]; Basefileprovider+="\\"; } stringFilePath = Basefileprovider +"raypi.entity"; if(Iservice.createentity (EntityName, FilePath))return Newmessagemodel<string> {Success =true, MSG ="Build succeeded" }; Else return Newmessagemodel<string> {Success =false, MSG ="Build Failed" }; } }}
ENTITYBLL
Here get the Contentrootpath main project address, so I did a little arithmetic.
It always feels like the. NET core takes a lot of effort to get the project path compared to. NET Framwork, and the various ways to get it will be followed by a separate chapter. If we find that this is more convenient than the current implementation method, I will be updated in a timely manner.
The following F5 run debugging and test in swagger.
Enter the table name (book), click Excute, and return to "build succeeded"
Back in the project, open the raypi.entity and find a more entity class than before--book.cs
It also generates the attributes (default, primary key, self-growth, etc.) of the annotations and fields we write for the field.
Here, the ability to automatically generate entity classes is implemented,
After we have integrated the Layui front-end framework into the project, it integrates the functionality into the backend management system, enabling the creation of entity classes in the background by filling in the data table name + click button.
"Build your own. NET Core API Framework from scratch" (iii) integrated lightweight orm--sqlsugar:3.3 Auto-generated entity classes