Separate POCO Object classes and DBContext from Entity Framework 6 Model(从 Entity Framework 6 模型中分离 POCO 对象类和 DBContext)
问题描述
我开始使用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 Generatoritem to theDBContextproject. - Created a new Class Library Project. This project will contain the
POCOobjects. - Moved the
[Project Name]Model.ttfile from theDbContextproject to thePOCOproject. - Edited the
[Project Name]Model.ttfile. On line 5, I changed:
const string inputFile = @"SampleModel.edmx";
to:
const string inputFile = @"..DbContextSampleModel.edmx"; - Added a reference in the
DbContextproject to thePOCOproject.
如果您使用的是 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
.ttfile. - Right-click the
.ttfile in the Solution Explorer and select "Debug T4 Template".
这篇关于从 Entity Framework 6 模型中分离 POCO 对象类和 DBContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Entity Framework 6 模型中分离 POCO 对象类和 DBContext
基础教程推荐
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
