How to use nodejs to write a data table entity class generation tool for C#

How to use nodejs to write a data table entity class generation tool for C#

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:
  • NodeJs high memory usage troubleshooting actual combat record
  • Detailed explanation of using Nodejs built-in encryption module to achieve peer-to-peer encryption and decryption
  • Detailed explanation of asynchronous iterators in nodejs
  • Detailed explanation of nodejs built-in modules
  • Nodejs module system source code analysis
  • A brief discussion on event-driven development in JS and Nodejs
  • How to use module fs file system in Nodejs
  • Summary of some tips for bypassing nodejs code execution
  • Nodejs Exploration: In-depth understanding of the principle of single-threaded high concurrency
  • Nodejs error handling process record

<<:  Complete steps to configure basic user authentication at the Nginx level

>>:  MySQL 8.0.11 Installation Tutorial under Windows

Recommend

Vue project realizes login and registration effect

This article example shares the specific code of ...

Linux CentOS 6.5 Uninstall, tar and install MySQL tutorial

Uninstall the system-provided MySQL 1. Check whet...

Detailed explanation of the workbench example in mysql

MySQL Workbench - Modeling and design tool 1. Mod...

Detailed explanation of MySQL's FreeList mechanism

1. Introduction After MySQL is started, BufferPoo...

How to fix the four sides of the table to scroll up, down, left and right

question: When I was doing project statistics rec...

How to create a new user in CentOS and enable key login

Table of contents Create a new user Authorize new...

Introduction to Linux common hard disk management commands

Table of contents 1. df command 2. du command 3. ...

React's transition from Class to Hooks

Table of contents ReactHooks Preface WhyHooks? Fo...

Use HTML to write a simple email template

Today, I want to write about a "low-tech&quo...

Basic tutorial on controlling Turtlebot3 mobile robot with ROS

Chinese Tutorial https://www.ncnynl.com/category/...

Linux disk space release problem summary

The /partition utilization of a server in IDC is ...

Data URI and MHTML complete solution for all browsers

Data URI Data URI is a scheme defined by RFC 2397...

Examples of importing and exporting MySQL table data

This article describes the import and export oper...

In-depth understanding of mathematical expressions in CSS calc()

The mathematical expression calc() is a function ...