专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > .NET Framework

求教EntityFramework 6.0 TT模板的使用,该怎么处理

发布时间:2011-06-23 13:57:53 文章来源:www.iduyao.cn 采编人员:星星草
求教EntityFramework 6.0 TT模板的使用
用EF.Utility.CS.ttinclude我按照这样写代码的话,会报错.

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

const string inputFile = @"DataModel.edmx";
var textTransform = DynamicTextTransformation.Create(this);
var code = new CodeGenerationTools(this);
var ef = new MetadataTools(this);
var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
var loader = new EdmMetadataLoader(textTransform.Host, textTransform.Errors);
var itemCollection = loader.CreateEdmItemCollection(inputFile);
var modelNamespace = loader.GetModelNamespace(inputFile);
var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);

var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
if (container == null)
{
    return string.Empty;
}
#>

//使用TT模板生成代码的片段
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
..........................................
.................................


报4个错如下
1.未能找到类型或命名空间名称“TypeMapper”(是否缺少 using 指令或程序集引用?)
2.未能找到类型或命名空间名称“CodeStringGenerator”(是否缺少 using 指令或程序集引用?)
3.非静态字段、方法或属性“System.Data.Entity.Core.Metadata.Edm.ItemCollection.GetItems<T>()”要求对象引用
4.正在编译转换: 当前上下文中不存在名称“ArgumentNotNull”


如果使用EF.Utility.CS.ttinclude而不使用EF6.Utility.CS.ttinclude 这样写的话.可以正常编译但是有个警告.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ include file="EF.Utility.CS.ttinclude"#>  //引进TT模板的命名空间
<#@ 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 = @"DataModel.edmx";

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

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>

//使用TT模板生成代码的片段
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
............................
.........................................


警告内容如下(原因是DataModel.edmx默认使用的是EF6.Utility.CS.ttinclude,所以警告版本不对):
1.正在运行转换: 输入文件似乎使用了此模板所不支持的架构版本。此可能会导致编译错误。请使用“添加新的生成项”以添加更新的模板。


所以我想除去这个警告 把EF.Utility.CS.ttinclude换成EF6.Utility.CS.ttinclude 改用EF6的TT模板.

请各位不吝赐教.

------解决思路----------------------
打开模型浏览器, 在空白地方右键, 添加代码生成器..
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: