我在http://www.haneycodes.net/automatically-generate-pocos-from-db-with-t4/找到了以下与T4 Template
相关联的SqlServer database and generates POCO classes from the tables
。我想用MySql
做同样的事情,但我不确定我需要改变什么。我尝试只使用MySqlConnection连接,但这不起作用。我是否必须创建MySql Server实例?如果有人知道用MySQL创建POCO的替代方法,我也对此持开放态度。
<#@ template language="C#" hostspecific="true" debug="True" #>
<#@ assembly name="System.Core" #> <#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#
string sqlServer = "9.9.9.9";
string sqlLogin = "admin";
string sqlPassword = "password";
string sqlDatabase = "MyDatabase";
string classNamespace = "Your.Namespace.Here";
string destinationFolder = "PocoFolder";
Server server = new Server(sqlServer);
server.ConnectionContext.LoginSecure = false;
server.ConnectionContext.Login = sqlLogin;
server.ConnectionContext.Password = sqlPassword;
server.ConnectionContext.Connect();
foreach (Table table in server.Databases[sqlDatabase].Tables)
{
if (table.Name.StartsWith("sys"))
{
continue;
} #>
using System;
namespace <#= classNamespace #>
{
public class <#= table.Name #>
{ <#
int columnCount = table.Columns.Count;
int i = 0;
foreach (Column col in table.Columns)
{
i++;
string propertyType = GetNetDataType(col.DataType.Name);
// If we can't map it, skip it
if (string.IsNullOrWhiteSpace(propertyType))
{
// Skip
continue;
}
// Handle nullable columns by making the type nullable
if (col.Nullable && propertyType != "string")
{
propertyType += "?";
} #>
public <#= propertyType #> <#= col.Name #> { get; set; } <#
// Do we insert the space?
if (i != columnCount)
{ #> <#
} #> <#
} #>
}
}
<#
// Write new POCO class to its own file
SaveOutput(table.Name + ".cs", destinationFolder);
} #>
<#+
public static string GetNetDataType(string sqlDataTypeName)
{ switch (sqlDataTypeName.ToLower())
{
case "bigint":
return "Int64";
case "binary":
case "image":
case "varbinary":
return "byte[]";
case "bit":
return "bool";
case "char":
return "char";
case "datetime":
case "smalldatetime":
return "DateTime";
case "decimal":
case "money":
case "numeric":
return "decimal";
case "float":
return "double";
case "int":
return "int";
case "nchar":
case "nvarchar":
case "text":
case "varchar":
case "xml":
return "string";
case "real":
return "single";
case "smallint":
return "Int16";
case "tinyint":
return "byte";
case "uniqueidentifier":
return "Guid";
default:
return null;
}
}
void SaveOutput(string outputFileName, string destinationFolder)
{
// Write to destination folder
string templateDirectory = Path.Combine(Path.GetDirectoryName(Host.TemplateFile), destinationFolder); string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.Delete(outputFilePath);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
// Flush generation
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
} #>
答案 0 :(得分:0)
我在VS extenion(db schema reader project)中使用了T4 Awesome来访问数据库结构。它支持任何ADO提供程序。您可以下载MySql提供程序here。 db模式阅读器项目上的文档非常好,因此您应该能够使用它们来确定如何连接到数据库并读取结构。
正如我所说,我在扩展程序中实现了这一点,所以如果你想要一个快速的解决方案,你可以download it尝试一下。如果没有,你几乎就像你已经拥有的那样,但是引用了db schema reader项目命名空间和程序集。