在不同项目中生成 POCO 类到具有实体框架模型的项目
我正在尝试使用 VS2010 的 EF4 存储库模式。
为此,我通过右键单击实体模型设计器并单击添加代码生成项来使用 POCO 代码生成。然后我选择 POCO 模板并获取我的课程。
我希望能够做的是将我的解决方案构建为实体(POCO)类的单独项目以及实体模型和存储库代码的另一个项目。
这意味着我的 MVC 项目可以将 POCO 类用于强类型视图等,而不必了解存储库或必须引用它。
为了将它们整合在一起,我将有另一个带有接口的单独项目并使用 IoC。
在我看来听起来不错,我只是不知道如何将类生成到他们自己的项目中!我可以复制它们,然后更改它们的命名空间,但每当我更改数据库中的架构并想要更新我的模型时,我想避免手动工作。
谢谢
I'm trying to use the Repository Pattern with EF4 using VS2010.
To this end I am using POCO code generation by right clicking on the entity model designer and clicking Add code generation item. I then select the POCO template and get my classes.
What I would like to be able to do is have my solution structured into separate projects for Entity (POCO) classes and another project for the entity model and repository code.
This means that my MVC project could use the POCO classes for strongly typed views etc and not have to know about the repository or have to have a reference to it.
To plug it all together I will have another separate project with interfaces and use IoC.
Sounds good in my head I just don't know how to generate the classes into their own project! I can copy them and then change the namespaces on them but I wanted to avoid manual work whenever I change the schema in the db and want to update my model.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上 EF 4.0 中的 T4 模板是根据这种情况设计的:)
有 2 个模板:
您应该输入 ModelName。 tt 文件,只需更改模板以指向持久性感知项目中的 EDMX 文件。
我知道听起来很奇怪:现在有一个依赖项,但它是在 T4 生成时,而不是在编译时!那应该没问题吧?因为生成的 POCO 组件仍然完全不知道持久性。
参见步骤 5 &其中 6 个: http://blogs .msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx 了解更多信息。
希望这对
亚历克斯有帮助
Actually the T4 templates in EF 4.0 were designed with this scenario in mind :)
There are 2 templates:
You should put the ModelName.tt file in you POCO project, and just change the template to point to the EDMX file in the persistence aware project.
Sounds weird I know: There is now a dependency, but it is at T4 generation time, not at compile time! And that should be okay? Because the resulting POCO assembly is still completely persistence ignorant.
See steps 5 & 6 of this: http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx for more.
Hope this helps
Alex
@Nick,
关于亚历克斯提供的链接,还有一件事需要注意。一旦我将主 .tt 文件移动到另一个项目,从“.Context.tt”文件生成的文件将无法编译,因为它缺少对位于不同命名空间中的 POCO 文件的引用(因为我想要我的 ObjectContext 与 POCO 文件位于不同的域中)。我必须修改“.Context.tt”文件以具有
using Poco.Namespace
(其中Poco.Namespace
是生成 POCO 文件的命名空间的名称)。然后我的项目就可以编译了。乔尔
@Nick,
One other thing to note about the link that Alex gave. Once I moved my main .tt file to a different project, the file that was generated from the ".Context.tt" file would not compile because it was missing references to the POCO files that were located in a different namespace (because I wanted my ObjectContext to be in a different domain than my POCO files). I had to modify the ".Context.tt" file to had a
using Poco.Namespace
(wherePoco.Namespace
is the name of the namespace where the POCO files were generated). This then allowed my project to compile.Joel
对于 EF5 + DbContext 生成器:可以轻松地将 Name.Context.tt 移动到不同的项目。但是,您将需要引用模型类。您可以手动执行此操作,但这需要您在每次生成代码时进行更改。您还可以为这两个项目使用相同的命名空间。这是有效的并且会起作用,但我认为这是糟糕的设计。另一种选择是更改 T4 模板 (Name.Context.tt)。
更改此(第 43 行):
为此:
这将检查您的模型名称空间是否与代码名称空间不同,如果是,它将插入所需的 using 来引用您的模型类。
For EF5 + DbContext generator: It's easy to move your Name.Context.tt to a different project. However, you will need to reference the model classes. You can do this manually, but this will require you to change it every time code gets generated. You could also use the same namespace for both projects. This is valid and will work, but I think this is bad design. Another alternative is to change the T4 template (Name.Context.tt).
Change this (line 43):
To this:
This will check if your model namespace differs from your code namespace, if so, it will insert the required using to reference your model classes.