使用实体框架模型将不同项目中的POCO类生成到项目中 [英] Generate POCO classes in different project to the project with Entity Framework model

查看:31
本文介绍了使用实体框架模型将不同项目中的POCO类生成到项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 VS2010 在 EF4 中使用存储库模式.

I'm trying to use the Repository Pattern with EF4 using VS2010.

为此,我通过右键单击实体模型设计器并单击添加代码生成项来使用 POCO 代码生成.然后我选择 POCO 模板并获取我的课程.

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.

我希望能够将我的解决方案组织成实体 (POCO) 类的单独项目和实体模型和存储库代码的另一个项目.

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.

这意味着我的 MVC 项目可以将 POCO 类用于强类型视图等,而不必了解存储库或必须对其进行引用.

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.

为了将它们整合在一起,我将有另一个带有接口的单独项目并使用 IoC.

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.

谢谢

推荐答案

实际上 EF 4.0 中的 T4 模板就是针对这种情况设计的 :)

Actually the T4 templates in EF 4.0 were designed with this scenario in mind :)

有 2 个模板:

  • 实体本身的一个(即 ModelName.tt)
  • ObjectContext 的一个(即 ModelName.Context.tt)

您应该将 ModelName.tt 文件放在您的 POCO 项目中,只需将模板更改为指向持久性感知项目中的 EDMX 文件即可.

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.

我知道听起来很奇怪:现在有一个依赖项,但它是在 T4 生成时,而不是在编译时!那应该没问题吧?因为得到的POCO程序集还是完全无知的持久化.

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.

参见步骤 5 &其中 6 个:http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx 了解更多.

See steps 5 & 6 of this: http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx for more.

希望能帮到你

亚历克斯

这篇关于使用实体框架模型将不同项目中的POCO类生成到项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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