test 2
This commit is contained in:
370
Ewide.WorkOrderSys/Entity/SqlModelTpl.tt
Normal file
370
Ewide.WorkOrderSys/Entity/SqlModelTpl.tt
Normal file
@@ -0,0 +1,370 @@
|
||||
<#@ template language="C#" debug="True" hostspecific="True" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Data" #>
|
||||
<#@ assembly name="System.Data.DataSetExtensions" #>
|
||||
<#@ assembly name="$(SolutionDir)lib\EntityFramework.dll" #>
|
||||
<#@ assembly name="System.xml" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ import namespace="System.Data" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ import namespace="System.Data.SqlClient" #>
|
||||
<#@ include file="ModelAuto.ttinclude"#>
|
||||
|
||||
|
||||
<# var manager = new Manager(Host, GenerationEnvironment, true) { OutputPath = Path.GetDirectoryName(Host.TemplateFile)+"/DataBase/"}; #>
|
||||
<#
|
||||
string tableClass="";
|
||||
//所有表名称
|
||||
//string sqlGetTable = "SELECT Name FROM SysObjects Where XType='U' ORDER BY Name";
|
||||
DataTable dt = GetDataTable();
|
||||
//DataTable dtRelationTable = GetRelationTable();
|
||||
//所有表信息
|
||||
string selectQuery ="select syscolumns.name,systypes.name,syscolumns.length from syscolumns,systypes where syscolumns.xusertype=systypes.xusertype and syscolumns.id=object_id('@tableName')";
|
||||
DataTable datacloumn = new DataTable();
|
||||
string primaryKey ="";
|
||||
#>
|
||||
|
||||
<#
|
||||
foreach(System.Data.DataRow row in dt.Rows)
|
||||
{
|
||||
string objType=row["XType"].ToString().Trim();
|
||||
tableClass = GetClassName(row["name"].ToString());
|
||||
manager.StartBlock(tableClass+".cs");
|
||||
//获取表格
|
||||
datacloumn.Clear();
|
||||
datacloumn = GetDataTableCloumn(row["name"].ToString());
|
||||
primaryKey = GetKey(row["name"].ToString());
|
||||
#>
|
||||
//----------<#=row["name"].ToString()#>开始----------
|
||||
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
//using System.Data.Entity.ModelConfiguration;
|
||||
using Ewide.WorkOrderSys.Entity.Base;
|
||||
using System.Runtime.Serialization;
|
||||
namespace Ewide.WorkOrderSys.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据表实体类:<#= tableClass #>
|
||||
/// </summary>
|
||||
[Table("<#=tableClass #>")]
|
||||
public partial class <#= tableClass #><# if(objType=="U"){#>: BaseEntity<#}else{#>: BaseEntity<#}#>
|
||||
{
|
||||
<#
|
||||
List<string> listBaseColumn = new List<string>{"Id","UNID","CreateTime","CreateUserId","LastUpdateTime","IsDeleted","Sort"};
|
||||
foreach (DataRow dr in datacloumn.Rows)
|
||||
{
|
||||
string columnName=GetFormColumnName(dr["columnname"].ToString(),tableClass);
|
||||
//if(objType=="U"&&listBaseColumn.Contains(columnName))
|
||||
if(listBaseColumn.Contains(columnName))
|
||||
continue;
|
||||
#>
|
||||
/// <summary>
|
||||
/// <#=dr["comments"].ToString()==""?"无描述": dr["comments"].ToString().Replace("\r","").Replace("\n","").Replace("\"","") #>
|
||||
/// </summary>
|
||||
|
||||
[Display(Name="<#=dr["comments"].ToString()==""?"无描述": dr["comments"].ToString().Replace("\r","").Replace("\n","").Replace("\"","") #>")]
|
||||
<#
|
||||
if(dr["columnname"].ToString().ToLower()==primaryKey.ToLower())
|
||||
{#>[Key]
|
||||
<#}
|
||||
#>
|
||||
[Column("<#=columnName #>")]
|
||||
[DataMember]
|
||||
public <#= objType=="V"?ViewTransFromSqlType(dr["datatype"].ToString(),dr["data_length"].ToString(),dr["nullable"].ToString()):TransFromSqlType(dr["datatype"].ToString(),dr["data_length"].ToString(),dr["nullable"].ToString()) #> <#=columnName#> {get;set;}
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
|
||||
/**
|
||||
/// <summary>
|
||||
/// 数据表实体类Map:<#= tableClass #>
|
||||
/// </summary>
|
||||
public class <#= tableClass #>Map : EntityTypeConfiguration<<#= tableClass #>>
|
||||
{
|
||||
public <#= tableClass #>Map()
|
||||
{
|
||||
this.ToTable("<#= tableClass #>");
|
||||
<#
|
||||
foreach (DataRow dr in datacloumn.Rows)
|
||||
{
|
||||
string columnName=GetFormColumnName(dr["columnname"].ToString(),tableClass);
|
||||
if(dr["columnname"].ToString()==primaryKey)
|
||||
{#>
|
||||
this.HasKey(t => t.<#= columnName #>);
|
||||
<#}
|
||||
else
|
||||
{#>
|
||||
this.Property(t => t.<#=columnName #>).HasColumnName("<#=columnName #>")<#if(dr["nullable"].ToString()=="0"){#>.IsRequired()<#}#>;
|
||||
<#}
|
||||
}
|
||||
#>
|
||||
}
|
||||
}**/
|
||||
|
||||
}
|
||||
|
||||
//----------<#=tableClass#>结束----------
|
||||
|
||||
<# manager.EndBlock(); #>
|
||||
|
||||
<#
|
||||
} #>
|
||||
|
||||
<#
|
||||
manager.Process(true);
|
||||
#>
|
||||
|
||||
|
||||
<#+
|
||||
/// <summary>
|
||||
/// SQL[不完善,需要的自己改造]
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public string TransFromSqlType(string type,string data_length,string nullable)
|
||||
{
|
||||
if(type.ToLower()=="nvarchar2")
|
||||
{
|
||||
return "varchar2";
|
||||
}
|
||||
if(type.ToLower()=="uniqueidentifier")
|
||||
{
|
||||
return "Guid"+(nullable=="1"?"?":"");
|
||||
}
|
||||
//是主键 且 type是字符串的 改成guid
|
||||
if((type.ToLower()=="raw"&&data_length=="16"))
|
||||
{
|
||||
return "Guid"+(nullable=="1"?"?":"");
|
||||
}
|
||||
switch(type.ToLower())
|
||||
{
|
||||
case "int":
|
||||
case "int32":
|
||||
case "number":
|
||||
type="int"+(nullable=="1"?"?":"");
|
||||
break;
|
||||
case "varchar":
|
||||
case "varchar2":
|
||||
case "text":
|
||||
case "ntext":
|
||||
case "nvarchar":
|
||||
case "longtext":
|
||||
case "string":
|
||||
case "raw":
|
||||
case "nclog":
|
||||
case "nclob":
|
||||
case "clob":
|
||||
type="string";
|
||||
break;
|
||||
case "blob":
|
||||
type="byte[]";
|
||||
break;
|
||||
case "bit":
|
||||
case "boolean":
|
||||
type="bool";
|
||||
break;
|
||||
case "datetime":
|
||||
case "date":
|
||||
type="DateTime"+(nullable=="1"?"?":"");
|
||||
break;
|
||||
case "bigint":
|
||||
type="long";
|
||||
break;
|
||||
case "decimal":
|
||||
type="decimal"+(nullable=="1"?"?":"");
|
||||
break;
|
||||
|
||||
|
||||
|
||||
}
|
||||
return type;
|
||||
}
|
||||
public string ViewTransFromSqlType(string type,string data_length,string nullable)
|
||||
{
|
||||
if(type.ToLower()=="nvarchar2")
|
||||
{
|
||||
return "varchar2";
|
||||
}
|
||||
if(type.ToLower()=="uniqueidentifier")
|
||||
{
|
||||
return "Guid?";
|
||||
}
|
||||
//是主键 且 type是字符串的 改成guid
|
||||
if((type.ToLower()=="raw"&&data_length=="16"))
|
||||
{
|
||||
return "Guid?";
|
||||
}
|
||||
switch(type.ToLower())
|
||||
{
|
||||
case "int":
|
||||
case "int32":
|
||||
case "number":
|
||||
type="int?";
|
||||
break;
|
||||
case "varchar":
|
||||
case "varchar2":
|
||||
case "text":
|
||||
case "ntext":
|
||||
case "nvarchar":
|
||||
case "longtext":
|
||||
case "string":
|
||||
case "raw":
|
||||
case "nclog":
|
||||
case "nclob":
|
||||
case "clob":
|
||||
type="string";
|
||||
break;
|
||||
case "blob":
|
||||
type="byte[]";
|
||||
break;
|
||||
case "bit":
|
||||
case "boolean":
|
||||
type="bool";
|
||||
break;
|
||||
case "datetime":
|
||||
case "date":
|
||||
type="DateTime?";
|
||||
break;
|
||||
case "bigint":
|
||||
type="long?";
|
||||
break;
|
||||
|
||||
|
||||
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
public string GetFormColumnName(string fieldname,string tablename)
|
||||
{
|
||||
if(tablename.StartsWith("PRJ_STAGE_FORM_")){
|
||||
return fieldname;
|
||||
}
|
||||
return GetClassName(fieldname);
|
||||
}
|
||||
public string GetClassName(string tablename)
|
||||
{
|
||||
/**
|
||||
string[] tablestr = tablename.Split('_');
|
||||
string rslt = "";
|
||||
foreach (var str in tablestr)
|
||||
{
|
||||
rslt = rslt + TitleToUpper(str.ToLower());
|
||||
}
|
||||
return rslt;
|
||||
**/
|
||||
return tablename;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public string TitleToUpper(string str)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
return string.Empty;
|
||||
|
||||
char[] s = str.ToCharArray();
|
||||
char c = s[0];
|
||||
|
||||
if ('a' <= c && c <= 'z')
|
||||
c = (char)(c & ~0x20);
|
||||
|
||||
s[0] = c;
|
||||
|
||||
return new string(s);
|
||||
}
|
||||
|
||||
|
||||
string connectionstr ="data source=118.178.224.202;initial catalog=CompanyScore_Test;persist security info=True;user id=KYSQLSERVERADMIN;password=KYDBLogin20161103...;";
|
||||
|
||||
public DataTable GetDataTable()
|
||||
{
|
||||
DataTable dtrslt = QueryTable("SELECT Name,XType FROM SysObjects Where XType='U' ORDER BY Name");
|
||||
DataTable viewrslt = QueryTable("SELECT Name,XType FROM SysObjects Where XType='V' ORDER BY Name");
|
||||
|
||||
DataTable rslt = dtrslt.Copy();
|
||||
//添加DataTable2的数据
|
||||
foreach (DataRow dr in viewrslt.Rows)
|
||||
{
|
||||
rslt.ImportRow(dr);
|
||||
}
|
||||
|
||||
return rslt;
|
||||
}
|
||||
|
||||
public DataTable QueryTable(string sql)
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
SqlConnection con = new SqlConnection(connectionstr);
|
||||
con.Open();
|
||||
SqlCommand cmd = new SqlCommand(sql, con);
|
||||
SqlDataAdapter oda = new SqlDataAdapter();
|
||||
oda.SelectCommand = cmd;
|
||||
oda.Fill(ds);
|
||||
con.Close();
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
return ds.Tables[0];
|
||||
}
|
||||
|
||||
public DataTable GetDataTableCloumn(string datatable)
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
SqlConnection con = new SqlConnection(connectionstr);
|
||||
con.Open();
|
||||
SqlCommand cmd = new SqlCommand("select sc.id,sc.name columnname,st.name datatype,sc.length data_length,sc.isnullable nullable,isnull(ep.value,'') comments from syscolumns sc inner join systypes st on sc.xusertype=st.xusertype left join sys.columns c on sc.id=c.object_id and sc.name=c.name left join sys.extended_properties ep on sc.id=ep.major_id and c.column_id=ep.minor_id and ep.class =1 where sc.id=object_id('"+datatable+"')", con);
|
||||
SqlDataAdapter oda = new SqlDataAdapter();
|
||||
oda.SelectCommand = cmd;
|
||||
oda.Fill(ds);
|
||||
con.Close();
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
return ds.Tables[0];
|
||||
}
|
||||
|
||||
|
||||
public string GetKey(string table)
|
||||
{
|
||||
string rslt = "";
|
||||
string sql = @"select * from ( select a.name as FieldName,a.isnullable,c.name as FieldType,COLUMNPROPERTY(a.id,a.name,'IsIdentity') as isidentity,PK=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (SELECT name FROM sysindexes WHERE indid in(SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
|
||||
))) then 'true' else 'false' end from SysColumns a left JOIN systypes c on a.xusertype=c.xusertype where a.id=Object_Id('"+table+"') ) m where PK='true'";
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionstr))
|
||||
{
|
||||
SqlCommand command = new SqlCommand(sql, connection);
|
||||
connection.Open();
|
||||
SqlDataReader reader;
|
||||
reader = command.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
rslt = reader.GetString(0);
|
||||
}
|
||||
reader.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
return rslt;
|
||||
}
|
||||
|
||||
|
||||
#>
|
||||
|
||||
Reference in New Issue
Block a user