t4模板 mysql_.net core 用T4模板连接MySql生成实体类

本文介绍了如何在.NET Core项目中利用T4模板连接到MySQL数据库,自动生成实体类。通过调整数据库连接字符串并执行SQL查询获取表信息,然后循环遍历表结构,生成对应的C#类文件,将结果保存到项目中。
摘要由CSDN通过智能技术生成

741637eaa1e39283a1c2011b50b0e390.png .net core 用T4模板连接MySql生成实体类标题

4,把MySql.Data.dll放在项目根目录,也可以自行更改。

在之前参考那个博友的文章,他的是连接SQL server的。

下面是参考他的T4模板,进行了修改的:

//MySql.Data.dll需放在项目根目录也可以自行更改

//    声明字段

MySqlConnection conn;

string sql;

MySqlCommand command;

MySqlDataReader reader;

ListtablesName;

ListtablesMark;

MySqlDataReader markReader;        //用来读取字段注释的Reader

string templateFileDir = Path.GetDirectoryName(Host.TemplateFile); //获取tt模板目录

//    数据库连接

string strConn = "Server=localhost;Database=tianya;User=root;Password=root;charset=utf8;";        //数据库连接字符串-这个换了环境就要改

conn = new MySqlConnection(strConn);

conn.Open();

//    查MySql数据库中有多少张表的sql语句

sql = "show table status";

command = new MySqlCommand(sql, conn);

reader = command.ExecuteReader();

tablesName = new List();

tablesMark = new List();

//    把表集合赋值到tablesName

while (reader.Read())

{

tablesName.Add(reader[0].ToString());        //获取表名称

tablesMark.Add(reader[17].ToString());        //获取表注释

}

reader.Close();

//    获取Visual Studio实例

EnvDTE.DTE dte = (EnvDTE.DTE)

((IServiceProvider)Host).GetService(typeof(EnvDTE.DTE));

EnvDTE.ProjectItem templateProjectItem =

dte.Solution.FindProjectItem(Host.TemplateFile);

//    循环映射模型文件

for (int i =0; i < tablesName.Count ; i++)

{

ListcolumsName = new List();

ListcolumsType = new List();

//    获取数据库表的字段的名称

sql = string.Format("SHOW COLUMNS FROM {0}", tablesName[i]);

command.CommandText = sql;

reader = command.ExecuteReader();

while (reader.Read())

{

columsName.Add(reader.GetString(0));

}

reader.Close();

//    获取数据库表的字段的类型

sql = string.Format("SELECT * FROM {0} LIMIT 0", tablesName[i]);

command.CommandText = sql;

reader = command.ExecuteReader();

reader.Read();

for(int z = 0 ; z < reader.FieldCount ; z++){

Type type =reader.GetFieldType(z);

columsType.Add(type);

}

reader.Close();

#>

//------------------------------------------------------------------------------

// //     此代码由T4模板自动生成

//       生成时间

//     对此文件的更改可能会导致不正确的行为,并且如果

//     重新生成代码,这些更改将会丢失。

//

//------------------------------------------------------------------------------

using System;

using System.Collections.Generic;

/// ///

///

public class {

for(int j = 0; jsql = string.Format("show full fields from {0} where Field = '{1}'" ,

tablesName[i] , columsName[j].ToString());

command.CommandText = sql;

markReader = command.ExecuteReader();

markReader.Read();

#>

/// ///

///

public   { get; set;}

} #>

}

byte[] byData = new byte[100];

char[] charData = new char[1000];

//    此地址与T4模板同目录

string path = templateFileDir +"\\"+tablesName[i]+".cs";

try

{

//    将生成的文件添加到T4模板下关联

File.WriteAllText(path , this.GenerationEnvironment.ToString());

templateProjectItem.ProjectItems.AddFromFile(path);

}

catch (IOException e)

{

Console.WriteLine(e.ToString());

}

this.GenerationEnvironment.Clear();

//↑↑清空T4模板当前的 文本模板转换进程(T4)用于组合生成的文本输出的字符串↑↑

}

command.Dispose();

conn.Close();

#>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值