.NetCore中使用PetaPoco,T4生成模版
2023年7月13日发(作者:)
.NetCore中使⽤PetaPoco,T4⽣成模版话不多说,直接上源码。1.引⽤NuGet2.添加T4<#@ template debug="true" hostspecific="false" language="C#" #><#@ assembly name="" #><#@ assembly name=""#><#@ import namespace="" #><#@ import namespace="" #><#@ import namespace="c" #><#@ import namespace="" #><#@ import namespace="ent" #><#@ output extension=".cs" #><# string Namespace="添加命名空间"; string RepoName="添加类名"; string ConnectionStr="添加连接串"; var builder = new T4Builder("Data Source=添加数据库连接");#>using e;using System;using c;using ent;namespace <#=Namespace #>{ public partial class <#=RepoName #> : Database { /// /// open the connection /// /// private static SqlConnection OpenConnection() { SqlConnection conn = new SqlConnection("添加连接串"); (); return conn; } private static SqlConnection OpenConnection(string name) { SqlConnection conn = new SqlConnection(ad(name)); (); 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 tance(); else return new <#=RepoName #>(); } [ThreadStatic] static <#=RepoName #> _instance; public override void OnBeginTransaction() { if (_instance == null) _instance = this; } public override void OnEndTransaction() { if (_instance == this) _instance = null; } public class Record where T : new() { public static <#=RepoName #> repo { get { return <#=RepoName #>.GetInstance(); } } public bool IsNew() { return (this); } public object Insert() { return (this); } public void Save() { (this); } public int Update() { return (this); } public int Update(IEnumerable columns) { return (this, columns); } public static int Update(string sql, params object[] args) { return (sql, args); } public static int Update(Sql sql) { return (sql); } public int Delete() { return (this); } public static int Delete(string sql, params object[] args) { return (sql, args); } public static int Delete(Sql sql) { return (sql); } public static int Delete(object primaryKey) { return (primaryKey); } public static bool Exists(object primaryKey) { return (primaryKey); } public static T SingleOrDefault(object primaryKey) { return OrDefault(primaryKey); } public static T SingleOrDefault(string sql, params object[] args) { return OrDefault(sql, args); } public static T SingleOrDefault(Sql sql) { return OrDefault(sql); } public static T FirstOrDefault(string sql, params object[] args) { return rDefault(sql, args); } public static T FirstOrDefault(Sql sql) { return rDefault(sql); } public static T Single(object primaryKey) { return (primaryKey); } public static T Single(string sql, params object[] args) { return (sql, args); } public static T Single(Sql sql) { return (sql); } public static T First(string sql, params object[] args) { return (sql, args); } public static T First(Sql sql) { return (sql); } public static List Fetch(string sql, params object[] args) { return (sql, args); } public static List Fetch(Sql sql) { return (sql); } public static List Fetch(int page, int itemsPerPage, string sql, params object[] args) { return (page, itemsPerPage,sql, args); } public static List SkipTake(int skip, int take, string sql, params object[] args) { return ke(skip, take, sql, args); } public static List SkipTake(int skip, int take, Sql sql) { return ke(skip, take, sql); } public static Page Page(int page, int itemsPerPage, string sql, params object[] args) { return (page, itemsPerPage,sql, args); } public static Page Page(int page, int itemsPerPage, Sql sql) { return (page, itemsPerPage, sql); } public static IEnumerable Query(string sql, params object[] args) { return (sql, args); } public static IEnumerable Query(Sql sql) { return (sql); } } }<# foreach(var item in ) {#>
[TableName("<#=ame#>")] <# if(!OrWhiteSpace(y)) { #>[PrimaryKey("<#=y #>")] <# } #>[ExplicitColumns] public partial class <#=ameStr#>:<#=RepoName#>.Record<<#=ameStr#>> {
<# foreach(var col in ) { #>[Column] public <#=Type#> <#=Name#> {get;set;} <# } #> }<# }#>}<#+ public class T4Builder { public List TableList = new List(); public List Table = new List(); public T4Builder(string conectionstring) { SqlConnection con = new SqlConnection(conectionstring); (); SqlCommand cmd = new SqlCommand("select name from sysobjects where xtype='u'", con); var reader = eReader(); while (()) { var tablename = ue(0).ToString(); (tablename); } foreach (var tablename in TableList) { var TableFrame = new TableFrame(); ame = "dbo." + tablename; #region 获取主键 SqlCommand cmd_prikey = new SqlCommand("EXEC sp_pkeys @table_name='" + tablename + "' ", con); var key_result = cmd_eReader(); while (key_()) { y = key_ue(3) != null ? key_ue(3).ToString() : null; } #endregion #region 获取列名 SqlCommand cmd_table = new SqlCommand("select COLUMN_NAME,DATA_TYPE,NUMERIC_SCALE,IS_NULLABLE frominformation_s where TABLE_NAME='" + tablename + "'", con); var table_result = cmd_eReader(); List Column = new List(); while (table_()) { Colum Columindex = new Colum(); Name = table_ue(0) != null ? table_ue(0).ToString() : null; if (!OrEmpty(Name)) { var ColumTypeStr = GetPropertyType(table_ue(1) != null ? table_ue(1).ToString() : null,table_ue(2) != null ? table_ue(2).ToString() : null); if (table_ue(3).ToString() == "YES" && ColumTypeStr != "string" && ColumTypeStr != "Guid") { ColumTypeStr = ColumTypeStr + "?"; } Type = ColumTypeStr; (Columindex); } } #endregion = Column; (TableFrame); } (); e(); } private string GetPropertyType(string sqlType, string dataScale) { string sysType = "string"; sqlType = r(); switch (sqlType) { case "bigint": sysType = "long"; break; case "smallint": sysType = "short"; break; case "int": sysType = "int"; break; case "uniqueidentifier": sysType = "Guid"; break; case "smalldatetime": case "datetime": case "date": sysType = "DateTime"; break; case "float": sysType = "double"; break; case "real": case "numeric": case "smallmoney": case "decimal": case "money": case "number": sysType = "decimal"; break; case "tinyint": sysType = "byte"; break; case "bit": sysType = "bool"; break; case "image": case "binary": case "varbinary": case "timestamp": sysType = "byte[]"; break; } if (sqlType == "number" && dataScale == "0") return "long"; return sysType; } } public class TableFrame { public string TableName { get; set; } public string Primkey { get; set; } public List Column { get; set; } public string TableNameStr { get { return TableName != null ? e("dbo.", "").Trim() : null; } } } public class Colum { public string ColumnName { get; set; } public string ColumnType { get; set; } }#>好到这T4模版在Core中⽣成 PatePoco模版已经结束了。这个也是⼀个简单的版本,希望⼤家有个参考随时沟通使⽤中的问题。
突然发现了⼀个⼩坑,每次conn都实例化⼀次,改进了下。 private static SqlConnection con; /// /// open the connection /// /// private static SqlConnection OpenConnection() { if (con == null) { con = new SqlConnection(ad("CGTSettings", "cgtTravelConnection")); } else { if ( == ) (); } return con; } private static SqlConnection OpenConnection(string name) { if (con == null) { con = new SqlConnection(ad(name)); } else { if ( == ) (); } return con; }
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689249717a225751.html
评论列表(0条)