利用T4模版生成EF实体

直接上代码,只需要修改EF实体的地址就可以了。

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#>
 
<#

CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);

string inputFile = @"..\Search.Model\SearchEngines.edmx";  //修改此处

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

#>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Search.Model;
using Search.IDal;
namespace Search.Dal
{
<#
// Emit Entity Types

foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
    //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
    //BeginNamespace(namespaceName, code);    
#>		
	public partial class <#=entity.Name#>Dal :BaseDal<<#=entity.Name#>>,I<#=entity.Name#>Dal
    {

    }
<#}#>
	
}

  

原文地址:https://www.cnblogs.com/dazhuangtage/p/5245361.html