从生成数据库表的实体类 [英] Generate entity class from database table

查看:156
本文介绍了从生成数据库表的实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要以这种形式来构建一个类

I need to build a class in this form

namespace BestCompanyEver
{
    public class PersonInfo
    {
    	public string Name{get;set;}
    	public int Age{get;set;}
    }
}

这是有列名字和年龄。一张桌子人

From a table Person that has columns Name and Age.

有没有准备好使用的解决方案呢?

Are there any ready to use solutions for this?

我知道我可以用T4或codesmith实现这一点,但应该有别人谁已经做到了。

I know I can implement this with T4 or Codesmith, but there should be somebody who already did it.

推荐答案

我发现了一些不错的T4模板,我可以使用。它从codePLEX的项目。

I found some nice T4 template I could use. Its from a project on codeplex.

的LINQ to SQL模板T4

模板是很难读,我花了一段时间来简化它。 在你可以使用它,你必须下载该项目的包括(CSharpDataClasses.tt)。

The template is hard to read an it took me a while to simplify it. Before you can use it you have to download an include (CSharpDataClasses.tt ) from the project.

下面是我的模板():

		<# // L2ST4 - LINQ to SQL templates for T4 v0.82 - http://www.codeplex.com/l2st4
		// Copyright (c) Microsoft Corporation.  All rights reserved.
		// This source code is made available under the terms of the Microsoft Public License (MS-PL)
		#><#@ template language="C#v3.5" hostspecific="True"
		#><#@ include file="L2ST4.ttinclude"
		#><#@ output extension=".generated.cs"
		#><# // Set options here
		var options = new {
			DbmlFileName = Host.TemplateFile.Replace(".tt",".dbml"), // Which DBML file to operate on (same filename as template)
			SerializeDataContractSP1 = false, // Emit SP1 DataContract serializer attributes
			FilePerEntity = true, // Put each class into a separate file
			StoredProcedureConcurrency = false, // Table updates via an SP require @@rowcount to be returned to enable concurrency
			EntityFilePath = Path.GetDirectoryName(Host.TemplateFile) // Where to put the files	
		};
		var code = new CSharpCodeLanguage();
		var data = new Data(options.DbmlFileName);
		var manager = new Manager(Host, GenerationEnvironment, true) { OutputPath = options.EntityFilePath };
		data.ContextNamespace = (new string[] { manager.GetCustomToolNamespace(data.DbmlFileName), data.SpecifiedContextNamespace, manager.DefaultProjectNamespace }).FirstOrDefault(s => !String.IsNullOrEmpty(s));
		data.EntityNamespace = (new string[] { manager.GetCustomToolNamespace(data.DbmlFileName), data.SpecifiedEntityNamespace, manager.DefaultProjectNamespace }).FirstOrDefault(s => !String.IsNullOrEmpty(s));
		manager.StartHeader();

		manager.EndHeader();
		var tableOperations = new List<TableOperation>();
			foreach(var table in data.Tables)
				tableOperations.AddRange(table.Operations);
			foreach(Table table in data.Tables)
				foreach(OperationType operationType in Enum.GetValues(typeof(OperationType)))
					if (!tableOperations.Any(o => (o.Table == table) && (o.Type == operationType))) {}
		if (!String.IsNullOrEmpty(data.ContextNamespace)) {}
		foreach(Table table in data.Tables) {
			foreach(TableClass class1 in table.Classes) {
				manager.StartBlock(Path.ChangeExtension(class1.Name + "Info" ,".cs"));
				if (!String.IsNullOrEmpty(data.EntityNamespace)) {#>
		using System;
		using System.Collections.Generic;
		using System.Linq;
		using System.Text;
		namespace <#=data.EntityNamespace#>
		{	
		<#		}

		#>	<#=code.Format(class1.TypeAttributes)#> class <#=class1.Name#>Info
			{

		<#		int dataMemberIndex = 1;
				if (class1.Columns.Count > 0) {
		#><#			foreach(Column column in class1.Columns) {#>
				private <#=code.Format(column.StorageType)#> <#= "_" + char.ToLower(column.Member[0]) +  column.Member.Substring(1) #><# if (column.IsReadOnly) {#> = default(<#=code.Format(column.StorageType)#>)<#}#>;

				<#=code.Format(column.MemberAttributes)#><#=code.Format(column.Type)#> <#=column.Member#>
				{
					get { return <#= "_" + char.ToLower(column.Member[0]) +  column.Member.Substring(1) #>; }
					set {<#= "_" + char.ToLower(column.Member[0]) +  column.Member.Substring(1) #> = value;}
				}

		<#			}
				}
		#>
			}
		}
		<#		
				manager.EndBlock();
			}
		}
		manager.StartFooter();
		manager.EndFooter(); 
		manager.Process(options.FilePerEntity);#>

这篇关于从生成数据库表的实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆