从 Entity Framework 6 模型中分离 POCO 对象类和 DBContext [英] Separate POCO Object classes and DBContext from Entity Framework 6 Model

查看:13
本文介绍了从 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.

  1. 创建了一个类库项目并添加了一个 ADO.NET 实体数据模型.该项目将包含 DB Context.
  2. DBContext 项目添加了一个新的 EF6.x DbContext Generator 项.
  3. 创建了一个新的类库项目.该项目将包含 POCO 对象.
  4. [Project Name]Model.tt 文件从 DbContext 项目移至 POCO 项目.
  5. 编辑了 [项目名称]Model.tt 文件.在第 5 行,我更改了:
    const string inputFile = @"SampleModel.edmx";
    到:
    const string inputFile = @"..DbContextSampleModel.edmx";
  6. DbContext 项目中添加了对 POCO 项目的引用.
  1. Created a Class Library Project and added an ADO.NET Entity Data Model. This project will contain the DB Context.
  2. Added a new EF6.x DbContext Generator item to the DBContext project.
  3. Created a new Class Library Project. This project will contain the POCO objects.
  4. Moved the [Project Name]Model.tt file from the DbContext project to the POCO project.
  5. Edited the [Project Name]Model.tt file. On line 5, I changed:
    const string inputFile = @"SampleModel.edmx";
    to:
    const string inputFile = @"..DbContextSampleModel.edmx";
  6. Added a reference in the DbContext project to the POCO project.

如果您使用的是 VS 2013,您可以调试模板以查看您的相对路径是如何解析的.

If you are using VS 2013, you can debug the template to see how your relative path is being resolved.

  1. .tt 文件中添加断点.
  2. 在解决方案资源管理器中右键单击 .tt 文件,然后选择Debug T4 Template".
  1. Add a breakpoint to your .tt file.
  2. Right-click the .tt file in the Solution Explorer and select "Debug T4 Template".

这篇关于从 Entity Framework 6 模型中分离 POCO 对象类和 DBContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆