问题描述
我开始使用Entity Framework 6.0.1 版本.我想将生成的 DbContext 和 POCO 模板类从模型中分离到不同的类库中.我花了几个小时解决了这个问题,但没有成功.
I started to use Entity Framework 6.0.1 version. I want to separate the generated DbContext and POCO template classes to different class library from the model. I spent a few hours solve the problem without any success.
如果我创建一个新的类库,添加 EF 6 EntityObject Generator 并填写以下模板变量:
If I create a new class library, add EF 6 EntityObject Generator and fill the following template variable:
SourceCsdlPath = @"....DataAccessModel.edmx"
,
构建后在错误列表中得到如下错误:
Get the following error in the error list after building:
错误 2 运行转换:System.IO.FileNotFoundException:找不到文件 文件名:'C:SourceEFsourcePOCO....DataAccessSZOSZRDBModel.edmx'
Error 2 Running transformation: System.IO.FileNotFoundException: Unable to locate file File name: 'C:SourceEFsourcePOCO....DataAccessSZOSZRDBModel.edmx'
服务器堆栈跟踪:在Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(字符串路径)在System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtrmd, Object[] args, 对象服务器, Object[]&outArgs) 在System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage味精)
Server stack trace: at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)
在 [0] 处重新抛出异常:在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessagereqMsg,IMessage retMsg)在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData,Int32 类型)在Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.ResolvePath(字符串路径)在Microsoft.VisualStudio.TextTemplating31479401930D2C4820ACF71C66B5389A24A5053726798D9718DB676B3FFA30A3454B3CB1EDE2E1C267D5278B1528860C072E81A0E4647BC23FCSettings993669604048用户设置)在Microsoft.VisualStudio.TextTemplating31479401930D2C4820ACF71C66B5389A24A5053726798D9718DB676B3FFA30A3454B3CB1EDE2E1C267D5278B1528860C072E81A0E4647BC23993669604048Text
Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.ResolvePath(String path) at Microsoft.VisualStudio.TextTemplating31479401930D2C4820ACF71C66B5389A24A5053726798D9718DB676B3FFA30A3454B3CB1EDE2E1C267D5278B1528860C072E81A0E4647BC23993669604048FCD.GeneratedTextTransformation.ApplyUserSettings(UserSettings userSettings) at Microsoft.VisualStudio.TextTemplating31479401930D2C4820ACF71C66B5389A24A5053726798D9718DB676B3FFA30A3454B3CB1EDE2E1C267D5278B1528860C072E81A0E4647BC23993669604048FCD.GeneratedTextTransformation.TransformText()
错误信息很清楚,但我不知道如何设置没有完整绝对路径的模型路径.
The Error message is clear, but i do not know, how to set Model path without full absolute path.
我不确定,最好使用最新版本的实体框架...
I am not sure, using newest version of entity framework is the best idea...
推荐答案
不需要绝对路径.看起来您的相对路径不正确.我正在使用 EF6.1,并且我在一个单独的项目中有 POCO 类.这就是我让它在 VS 2013 中工作的方式.
Absolute paths are not required. It looks like your relative path is not correct. I am using EF6.1 and I have the POCO classes in a separate project. This is how I got it to work in VS 2013.
- 创建了一个类库项目并添加了一个
ADO.NET 实体数据模型
.该项目将包含DB Context
. - 向
DBContext
项目添加了一个新的EF6.x DbContext Generator
项. - 创建了一个新的类库项目.该项目将包含
POCO
对象. - 将
[Project Name]Model.tt
文件从DbContext
项目移至POCO
项目. - 编辑了
[项目名称]Model.tt
文件.在第 5 行,我更改了:const string inputFile = @"SampleModel.edmx";
到:const string inputFile = @"..DbContextSampleModel.edmx";
- 在
DbContext
项目中添加了对POCO
项目的引用.
- Created a Class Library Project and added an
ADO.NET Entity Data Model
. This project will contain theDB Context
. - Added a new
EF6.x DbContext Generator
item to theDBContext
project. - Created a new Class Library Project. This project will contain the
POCO
objects. - Moved the
[Project Name]Model.tt
file from theDbContext
project to thePOCO
project. - Edited the
[Project Name]Model.tt
file. On line 5, I changed:
const string inputFile = @"SampleModel.edmx";
to:
const string inputFile = @"..DbContextSampleModel.edmx";
- Added a reference in the
DbContext
project to thePOCO
project.
如果您使用的是 VS 2013,您可以调试模板以查看您的相对路径是如何解析的.
If you are using VS 2013, you can debug the template to see how your relative path is being resolved.
- 在
.tt
文件中添加断点. - 在解决方案资源管理器中右键单击
.tt
文件,然后选择Debug T4 Template".
- Add a breakpoint to your
.tt
file. - Right-click the
.tt
file in the Solution Explorer and select "Debug T4 Template".
这篇关于从 Entity Framework 6 模型中分离 POCO 对象类和 DBContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!