C# connects to the database to automatically generate entity classes

It is often encountered in projects to create entity classes. When the database is relatively large or the table structure needs to be modified frequently, the efficiency of manually creating entity classes is very low.

1. Handwritten code creation and generation of entity class tools

Before writing Java code, I was used to using tools such as Mybatis Generator to generate entity class code, so I wondered if I could also write an entity class generation tool in C#. In line with the principle of not reinventing the wheel, I searched the Internet and saw a very good tool: CreateEntityModel. Although this tool has no interface, the code is written very well. It adopts the builder mode to support different databases, which is convenient for its own expansion of new databases.
Since the original tools only supported MySQL and SQLServer databases, they could not meet my requirements, so I expanded on this code basis and added support for PostgresSql databases. The database connection tool uses the Npgsql class library.

The source code is placed on a well-known dating website: https://github.com/lordum/CreateEntityModel.git
Welcome everyone to communicate.
insert image description here

In addition, the address of the original author is also put up, everyone supports: https://github.com/LiuHuiGang/CreateEntityModel.git

2. Use the code generator FreeSql.Generator to generate

Code generator FreeSql.Generator is a code generator for FreeSql, which can generate entity classes and support dynamic generation of entities from database entities. There are two templates by default. Based on Razor, custom templates can be specified

  • 1 dotnet-tool install FreeSql.Generator
dotnet tool install -g FreeSql.Generator
  • 2 Create a new directory, enter cmd in the address bar to quickly open the command window, and enter the command:
FreeSql.Generator --help

The command-line tool has great advantages in generating entity classes, and the subsequent generation of coverage operations is equal to one-click completion, and supports Mac/Linux platforms.
The help description for the command is as follows:

C:\WINDOWS\system32>FreeSql.Generator --help
        ____                   ____         __
       / __/  ____ ___  ___   / __/ ___ _  / /
      / _/   / __// -_)/ -_) _\ \  / _ `/ / /
     /_/    /_/   \__/ \__/ /___/  \_, / /_/
                                    /_/


  # Github # https://github.com/2881099/FreeSql v2.0.105

    FreeSql 快速生成数据库的实体类

    更新工具:dotnet tool update -g FreeSql.Generator


  # 快速开始 #

  > FreeSql.Generator -Razor 1 -NameOptions 0,0,0,0 -NameSpace MyProject -DB "MySql,Data Source=127.0.0.1;..."

     -Razor 1                  * 选择模板:实体类+特性
     -Razor 2                  * 选择模板:实体类+特性+导航属性
     -Razor "d:\diy.cshtml"    * 自定义模板文件

     -NameOptions              * 4个布尔值对应:
                                 首字母大写
                                 首字母大写,其他小写
                                 全部小写
                                 下划线转驼峰

     -NameSpace                * 命名空间

     -DB "MySql,data source=127.0.0.1;port=3306;user id=root;password=root;initial catalog=数据库;charset=utf8;sslmode=none;max pool size=2"
     -DB "SqlServer,data source=.;initial catalog=数据库;User Id=sa;Password=123456;TrustServerCertificate=true;pooling=true;max pool size=2"
     -DB "PostgreSQL,host=192.168.164.10;port=5432;username=postgres;password=123456;database=数据库;pooling=true;maximum pool size=2"
     -DB "Oracle,user id=user1;password=123456;data source=//127.0.0.1:1521/XE;pooling=true;max pool size=2"
     -DB "Sqlite,data source=document.db"
     -DB "Firebird,database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=2"
     -DB "Dameng,server=127.0.0.1;port=5236;user id=2user;password=123456789;database=2user;poolsize=2"
     -DB "KingbaseES,server=127.0.0.1;port=54321;uid=USER2;pwd=123456789;database=数据库"
     -DB "ShenTong,host=192.168.164.10;port=2003;database=数据库;username=SYSDBA;password=szoscar55;maxpoolsize=2"
                               * Dameng(达梦数据库)、KingbaseES(人大金仓数据库)、ShenTong(神舟通用数据库)

     -Filter                   Table+View+StoreProcedure
                               默认生成:表+视图+存储过程
                               如果不想生成视图和存储过程 -Filter View+StoreProcedure

     -Match                    表名或正则表达式,只生成匹配的表,如:dbo\.TB_.+

     -FileName                 文件名,默认:{name}.cs
     -Output                   保存路径,默认为当前 shell 所在目录
                               推荐在实体类目录创建 gen.bat,双击它重新所有实体类

official document

Guess you like

Origin blog.csdn.net/zhoulizhu/article/details/123916644