生成POCO类在不同的项目与实体框架模型项目项目、实体、框架、模型

2023-09-02 01:27:28 作者:*当空\皓月/

我试图使用Repository模式使用VS2010 EF4。

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

要为此我使用POCO code一代通过右键点击实体模型设计器,然后单击添加code代项目。然后,我选择了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)班和另外一个项目为实体模型和信息库code。

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.

希望这有助于

亚历