求教EntityFramework 6.0 TT模板的使用

「已注销」 2015-04-14 07:11:27
用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模板.

请各位不吝赐教.
...全文
2031 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
watercity0 2016-06-13
  • 打赏
  • 举报
回复
「已注销」 2015-04-15
  • 打赏
  • 举报
回复
有大神知道吗?
「已注销」 2015-04-14
  • 打赏
  • 举报
回复
引用 1 楼 zujinsheng 的回复:
打开模型浏览器, 在空白地方右键, 添加代码生成器..
是自己添加的tt模板,生成代码.不是edmx生成的tt文件.
zujinsheng 2015-04-14
  • 打赏
  • 举报
回复
打开模型浏览器, 在空白地方右键, 添加代码生成器..
Paperback: 1625 pages Publisher: Apress; 7th ed. 2015 edition (January 1, 2016) Language: English ISBN-10: 1484213335 ISBN-13: 978-1484213339 This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including: A Refined ADO.NET Entity Framework Programming Model Numerous IDE and MVVM Enhancements for WPF Desktop Development Numerous updates to the ASP.NET Web APIs This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies). The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms. What you’ll learn Be the first to understand the .NET 4.6 platform and C# 6. Discover the ins and outs of the leading .NET technology. Learn from an award-winning author who has been teaching the .NET world since version 1.0. Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime. Who this book is for This book is perfect for anyone who is interested in the new .NET Framework 4.6 and the C# language. Whether you are moving to .NET for the first time or are already writing applications using previous .NET versions, this book will provide you with a comprehensive grounding in the new technology and serve as a complete reference throughout your coding career.

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧