Although Microsoft provides T4 templates, I find them very difficult to use. There is nothing like writing templates directly with scripts. Because I want to make peripheral tools for an old project, I need to connect to the database. I am used to using EntityFrameworkCore, because after all, it is the ORM I have been using since my debut. In the EF6 era, vs provided dbfirst, but it seemed to be only for sqlserver. Because the database this time is MySQL, many things in VS are not fully supported. But if the support is not enough, you can do it yourself to have enough food and clothing. We use the ejs template engine as the generator. npm install ejs Then use the query to get the table structure: b.query('desc posts').then(res => { }) Then write the template. The template syntax of ejs is very similar to the template syntax of the aspx era, both of which are angle brackets + percent signs <%%>. I believe that those who have aspx development experience are still very familiar with this template engine. using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace your namespace { public class <%= table -%> { <% rows.forEach(function(row){ -%> <% if(row.Type.indexOf('bigint')!=-1){ -%> public long <%= row.Field %> { get; set; } <% } -%> <% if(row.Type.indexOf('datetime')!=-1){ -%> public DateTime <%= row.Field %> { get; set; } <% } -%> <% if(row.Type.indexOf('varchar')!=-1){ -%> public string <%= row.Field %> { get; set; } <% } -%> <% if(row.Type.indexOf('mediumtext')!=-1){ -%> public string <%= row.Field %> { get; set; } <% } -%> <% if(row.Type.indexOf('bit')!=-1){ -%> public bool <%= row.Field %> { get; set; } <% } -%> <% if(row.Type.indexOf('longtext')!=-1){ -%> public string <%= row.Field %> { get; set; } <% } -%> <% }); -%> } } In the above template, corresponding C# type mappings are made for different MySQL data types. Then use ejs to render a text, and finally save it to the folder. var tableName = 'table name'; //The corresponding class name in the template and the name of the generated cs file ejs.renderFile('./template/posts.ejs', { rows: res.rows, 'table': tableName}, (err, str) => { if (err) { console.error(err); } else { let temp = path.join(__dirname, 'temp'); var exist = fs.existsSync(temp) if (!exist) { fs.mkdirSync() } fs.writeFile(path.join(temp, tableName+'.cs'), str, (err) => { if (err) { console.error(err); } else { console.log('Template generated successfully'); } }) } }) Use node to execute it and a cs file will be generated. Since I don't have many tables, I just generate them one by one. If you want to expand the generation of the entire database, you can write a few more lines of code to generate the entire library! The above is the details of how to use nodejs to write an entity class generation tool for a data table in C#. For more information about nodejs, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Complete steps to configure basic user authentication at the Nginx level
>>: MySQL 8.0.11 Installation Tutorial under Windows
This article example shares the specific code of ...
Uninstall the system-provided MySQL 1. Check whet...
MySQL Workbench - Modeling and design tool 1. Mod...
1. Introduction After MySQL is started, BufferPoo...
question: When I was doing project statistics rec...
Recently, I ran a spark streaming program in a ha...
Table of contents Create a new user Authorize new...
Table of contents 1. df command 2. du command 3. ...
Table of contents ReactHooks Preface WhyHooks? Fo...
Today, I want to write about a "low-tech&quo...
Chinese Tutorial https://www.ncnynl.com/category/...
The /partition utilization of a server in IDC is ...
Data URI Data URI is a scheme defined by RFC 2397...
This article describes the import and export oper...
The mathematical expression calc() is a function ...