首页 > 代码库 > 批量根据实体类生成相关的类

批量根据实体类生成相关的类

<#
// 从实体类生成相关的实体类
#>
<#@ template hostspecific="true" language="C#" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Configuration" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ include file="MultipleOutputHelper.ttinclude" #>
<#
//Multi output
var fileManager = Manager.Create(Host, GenerationEnvironment);

    //所引用的using
var modelSp = "Hanlly.MedicalEthics.Models.MedicalQualityDB";
var repSp = "Hanlly.MedicalEthics.Repository";
    var repBaseSp = "Hanlly.MedicalEthics.Repository.Infrastructure";
                   
    string modelDir = @"D:\work\yiliao\Hanlly.MedicalEthics.Models\MedicalQualityDB"; //需要生成的实体类文件所在的路径
var fileList = Directory.GetFiles(modelDir, "*.cs");
int fileCount = 0;
foreach (var file in fileList)
{
var fileInfo = new FileInfo(file);
var fileName = Path.GetFileNameWithoutExtension(file);
if(fileName.EndsWith("Context")){
continue;
}
var modelName = fileName;
fileName = fileName.Replace("_", "");
var repFileName = fileName + "Repository";
var repFileNameWithExtension = repFileName + ".cs";

//Repository Register
var register = "For<I" + repFileName + ">().Use<" + repFileName + ">();"; //生成的文件名


fileManager.StartNewFile(repFileNameWithExtension);


#>
/**********************************************************************************
*
*              文件名称 :<#= repFileNameWithExtension #>
*              作者     : HuangFL
*              创建时间 : <#= DateTime.Now.ToString("yyy-MM-dd hh:mm:ss") #>
*
************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using <#= modelSp #>;
using <#= repBaseSp #>;


namespace <#= repSp #>
{
    public interface I<#= repFileName #> : IRepository<<#= modelName #>>
    {


    }


    public class <#= repFileName #> : RepositoryBase<<#= modelName #>>, I<#= repFileName #>
    {
        public <#= repFileName #>(IDefaultDataContextFactory factory)
            : base(factory)
        {
        }
    }
}


<# 
        fileManager.EndBlock();
WriteLine("//" + register);
fileCount ++;
}


fileManager.Process(true); 
WriteLine("//一共生成文件数 : " + fileCount);
#>

批量根据实体类生成相关的类