C# SqlSugar 数据库 T4模板

生成效果

模板代码

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data"#>
<#@ import namespace="System.Data.SqlClient"#>
<#

// 连接字符串
string connectionString = "Server=DESKTOP-7IR5JSN;Database=BlazorApp;Integrated Security=False;User ID=sa;Password=asdf-1234;";

// 数据库连接
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

// 查询数据库中的表信息
string query = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'";
SqlCommand command = new SqlCommand(query, connection);
List<string> tables = new List<string>();

using (SqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        tables.Add(reader.GetString(0));
    }
}

// 生成模型类
foreach (var table in tables)
{
#>
namespace BlazorORM.Entity
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using SqlSugar;

    // 生成的模型类对应数据库表: <#= table #>
    [SugarTable("<#= table #>")]
    public class <#= table #>
    {
<#
        query = $"SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{table}'";
        command.CommandText = query;
        List<string> columns = new List<string>();

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                string columnName = reader.GetString(0);
                string columnType = reader.GetString(1);
#>
        // 对应数据库列:<#= columnName #>,类型:<#= columnType #>
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] // 可根据需要设置主键、自增等属性
        public <#= GetCSharpType(columnType) #> <#= columnName #> { get; set; }
<#
            }
        }
#>
    }
}
<#
}

// 关闭连接
connection.Close();

// 将数据库类型映射为 C# 类型
string GetCSharpType(string dbType)
{
    // 可根据数据库类型自定义映射
    switch (dbType)
    {
        case "int":
            return "int";
        case "nvarchar":
            return "string";
        // 添加其他数据库类型映射
        default:
            return "object";
    }
}
#>

相关推荐

  1. Nanopc T4 使用OpenCV

    2023-12-26 05:50:01       38 阅读
  2. 【RK3288 Android6 T2pro 支持移远和有方4G模块切换】

    2023-12-26 05:50:01       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-26 05:50:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-26 05:50:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-26 05:50:01       82 阅读
  4. Python语言-面向对象

    2023-12-26 05:50:01       91 阅读

热门阅读

  1. Vue学习常见问答

    2023-12-26 05:50:01       59 阅读
  2. 使用easyexcel对导出表格添加合计行

    2023-12-26 05:50:01       55 阅读
  3. 软件研发--“扭曲变形正成为一种常态”

    2023-12-26 05:50:01       50 阅读
  4. Rust 软件测试

    2023-12-26 05:50:01       55 阅读
  5. VTK学习笔记(四十二)vtk绘制箭头

    2023-12-26 05:50:01       60 阅读