教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 实用模板 >

mvc实用架构设计(三)ef-code first(3):使用t4模板生成相似代(5)

来源:网络收集 时间:2026-08-03
导读: 81 configurations.Add(this); 82 } 83 } 84 } 85 #+ 86 return this.GenerationEnvironment.ToString(); 87 } 88 } 89 # 生成模板调用方 EntityCodeScript.tt 定义 1 #@ template language=\ 2 #@ output extensio

81 configurations.Add(this); 82 } 83 } 84 } 85 <#+

86 return this.GenerationEnvironment.ToString(); 87 } 88 }

89 #> 生成模板调用方 EntityCodeScript.tt 定义 1 <#@ template language=\ 2 <#@ output extension=\

3 <#@ Assembly Name=\ 4 <#@ Assembly

Name=\GMF.Component.Tools.dll\

5 <#@ import namespace=\ 6 <#@ Import Namespace=\ 7 <#@ Import Namespace=\ 8 <#@ import namespace=\ 9 <#@ Import

Namespace=\10 <#@ Import Namespace=\

#>

11 <#@ Import

Namespace=\12 <#@ include file=\13 <#@ include

file=\14 <#

15 string currentPath =

Path.GetDirectoryName(Host.TemplateFile); 16 string projectPath =currentPath.Substring(0, currentPath.IndexOf(@\

17 string solutionPath = currentPath.Substring(0, currentPath.IndexOf(@\18

19 string modelFile= Path.Combine(solutionPath, @\Models.dll\

20 byte[] fileData= File.ReadAllBytes(modelFile); 21 Assembly assembly = Assembly.Load(fileData); 22 IEnumerable<Type> modelTypes = assembly.GetTypes().Where(m => typeof(Entity).IsAssignableFrom(m)

&& !m.IsAbstract);

23 foreach(Type modelType in modelTypes) 24 {

25 T4ModelInfo model = new T4ModelInfo(modelType); 26 //实体映射类

27 EntityConfigurationTemplate config = new EntityConfigurationTemplate(model); 28 string path =

string.Format(@\29 config.Output.Encoding = Encoding.UTF8; 30 config.RenderToFile(Path.Combine(path, config.FileName)); 31 }

32 #> 调用方通过反射从业务实体程序集

GMF.Demo.Core.Models.dll 中获取所有基类为 Entity 的并且不是抽象类的实体类型信息,再调用模板逐个生成实体配置类文件。 例如,生成的登录记录信息(LoginLog)的映射文件 LoginLogConfiguration.generated.cs 如下: 1 //------------------------------------------------------------------------------

2 // <auto-generated>

3 // 此代码由工具生成。

4 // 对此文件的更改可能会导致不正确的行为,并且如果

5 // 重新生成代码,这些更改将会丢失。

6 // 如存在本生成代码外的新需求,请在相同命名空间下创建同名分部类实现

LoginLogConfigurationAppend 分部方法。 7 // </auto-generated> 8 //

9 // <copyright

file=\10 // Copyright(c)2013 GMFCN.All rights reserved.

11 // CLR版本:4.0.30319.239 12 // 开发组织:郭明锋@中国 13 // 公司网站:http://www.gmfcn.net 14 // 所属工程:GMF.Demo.Core.Data 15 // 生成时间:2013-06-16 17:45 16 // </copyright> 17

//------------------------------------------------------------------------------

18

19 using System;

20 using System.Data.Entity.ModelConfiguration; 21 using

System.Data.Entity.ModelConfiguration.Configuration; 22

23 using GMF.Component.Data; 24 using GMF.Demo.Core.Models; 25 26

27 namespace GMF.Demo.Core.Data.Configurations 28 {

29 /// <summary>

30 /// 实体类-数据表映射——登录记录信息 31 /// </summary>

32 internal partial class LoginLogConfiguration : EntityTypeConfiguration<LoginLog>, IEntityMapper 33 {

34 /// <summary>

35 /// 实体类-数据表映射构造函数——登录记录信息

36 /// </summary>

…… 此处隐藏:549字,全部文档内容请下载后查看。喜欢就下载吧 ……
mvc实用架构设计(三)ef-code first(3):使用t4模板生成相似代(5).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/520000.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)