引入PetaPoco及T4后创建模板的代码
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="MySql.Data.MySqlClient" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="自己的文件路径\MySql.Data.Dll" #>//找到自己MySql.Data.Dll文件所在路径,项目-->依赖项NuGet-->找到MySql.Data-->右击属性找到Path就是文件所在路径了
<#@ output extension=".cs" #>
<#
string ConnStr="Server=主机地址;Port=3306;Database=数据库名称;Uid=root;Pwd=密码;pooling=false;";
string Namespace="Model";
string RepoName="DB";
var builder = new T4Builder(ConnStr);
#>
using System;
using PetaPoco.NetCore;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
namespace <#=Namespace#>
{
public partial class <#=RepoName#> : Database
{
/// <summary>
/// open the connection
/// </summary>
/// <returns></returns>
private static MySqlConnection OpenConnection()
{
MySqlConnection conn = new MySqlConnection("<#=ConnStr#>");
conn.Open();
return conn;
}
private static MySqlConnection OpenConnection(string name)
{
MySqlConnection conn = new MySqlConnection(name);
conn.Open();
return conn;
}
public <#=RepoName#>() : base(OpenConnection())
{
CommonConstruct();
}
public <#=RepoName#>(string connectionStringName) : base(OpenConnection(connectionStringName))
{
CommonConstruct();
}
partial void CommonConstruct();
public interface IFactory
{
<#=RepoName #> GetInstance();
}
public static IFactory Factory { get; set; }
public static <#=RepoName #> GetInstance()
{
if (_instance != null)
return _instance;
if (Factory != null)
return Factory.GetInstance();
else
return new <#=RepoName #>();
}
[ThreadStatic] static <#=RepoName #> _instance;
public override void OnBeginTransa