从实体框架6型独立POCO对象类和的DbContext [英] Separate POCO Object classes and DBContext from Entity Framework 6 Model
问题描述
我开始使用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生成并填写以下模板变量:
If I create a new class library, add EF 6 EntityObject Generator and fill the following template variable:
SourceCsdlPath = @.. \ .. \数据访问\ Model.edmx
,
得到以下错误在错误列表建设后:
Get the following error in the error list after building:
错误2运行转换信息:System.IO.FileNotFoundException: 无法找到文件文件名: C:\来源\ EFsource \ POCO .... \数据访问\ SZOSZRDBModel.edmx
Error 2 Running transformation: System.IO.FileNotFoundException: Unable to locate file File name: 'C:\Source\EFsource\POCO....\DataAccess\SZOSZRDBModel.edmx'
服务器堆栈跟踪:在 Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(字符串 路径)在 System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr的 MD,对象[] ARGS,对象服务器,对象[]放大器; outArgs)在 System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(即时聊天 MSG)
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(即时聊天 reqMsg,即时聊天retMsg)在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&安培; MSGDATA,的Int32型)在 Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.ResolvePath(字符串 路径)在 Microsoft.VisualStudio.TextTemplating31479401930D2C4820ACF71C66B5389A24A5053726798D9718DB676B3FFA30A3454B3CB1EDE2E1C267D5278B1528860C072E81A0E4647BC23993669604048FCD.GeneratedTextTransformation.ApplyUserSettings(UserSettings userSettings)在 Microsoft.VisualStudio.TextTemplating31479401930D2C4820ACF71C66B5389A24A5053726798D9718DB676B3FFA30A3454B3CB1EDE2E1C267D5278B1528860C072E81A0E4647BC23993669604048FCD.GeneratedTextTransformation.TransformText()
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上下文
。 - 添加了一个新的
EF6.x的DbContext发电机
项的的DbContext
的项目。 - 创建一个新的类库项目。该项目将包含
POCO
的对象。 - 感动从
的DbContext
项目[项目名称] Model.tt
文件到POCO
的项目。 - 主编的
[项目名称] Model.tt
文件。在第5行,我改变:
常量字符串INPUTFILE = @SampleModel.edmx;
到:
常量字符串INPUTFILE = @.. \的DbContext \ SampleModel.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 = @"..\DbContext\SampleModel.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
文件。 - 右键单击在Solution Explorer中的
.TT
文件,然后选择调试T4模板。
- Add a breakpoint to your
.tt
file. - Right-click the
.tt
file in the Solution Explorer and select "Debug T4 Template".
这篇关于从实体框架6型独立POCO对象类和的DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!