.net 使用实体框架模型将不同项目中的POCO类生成到项目中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2464909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:05:54  来源:igfitidea点击:

Generate POCO classes in different project to the project with Entity Framework model

.netentity-frameworkcode-generationrepository-patternpoco

提问by Max

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

我正在尝试使用 VS2010 将 Repository Pattern 与 EF4 一起使用。

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 代码生成。然后我选择 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.

我希望能够做的是将我的解决方案结构化为实体(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.

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

To plug it all together I will have another separate project with interfaces and use IoC.

为了将它们整合在一起,我将有另一个带有接口的单独项目并使用 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

谢谢

采纳答案by Alex James

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

实际上 EF 4.0 中的 T4 模板是在设计时考虑到了这种情况:)

There are 2 templates:

有2个模板:

  • One for the Entities themselves (i.e. ModelName.tt)
  • One for the ObjectContext (i.e. ModelName.Context.tt)
  • 一个用于实体本身(即 ModelName.tt)
  • 一个用于 ObjectContext(即 ModelName.Context.tt)

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.

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

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.

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

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

有关更多信息,请参阅第 5 步和第 6 步:http: //blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx

Hope this helps

希望这可以帮助

Alex

亚历克斯

回答by hjoelr

@Nick,

@缺口,

  1. To force the regeneration of the POCO entities, you just need to right-click the main .tt file and select "Run Custom Tool". This will force it to regenerate your POCO classes with your updated changes to the .edmx model.
  2. Is there any problem with you going ahead and Right-clicking the model and selecting "Generate Database from Model..." even though you aren't necessarily generating the database? That will most likely get rid of your 'Error 11007...'.
  3. I think it's equivalent to a "Code Behind". I don't know any more than that.
  1. 要强制重新生成 POCO 实体,您只需右键单击主 .tt 文件并选择“运行自定义工具”。这将强制它使用您对 .edmx 模型的更新更改重新生成您的 POCO 类。
  2. 即使您不一定要生成数据库,您继续右键单击模型并选择“从模型生成数据库...”是否有任何问题?这很可能会消除您的“错误 11007...”。
  3. 我认为这相当于“代码隐藏”。我不知道更多。

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(where Poco.Namespaceis the name of the namespace where the POCO files were generated). This then allowed my project to compile.

关于亚历克斯提供的链接的另一件事要注意。一旦我将主 .tt 文件移动到不同的项目,从“.Context.tt”文件生成的文件将无法编译,因为它缺少对位于不同命名空间中的 POCO 文件的引用(因为我想要我的 ObjectContext 与我的 POCO 文件位于不同的域中)。我不得不修改“.Context.tt”文件以获得一个using Poco.Namespace(其中Poco.Namespace是生成 POCO 文件的命名空间的名称)。然后这允许我的项目编译。

Joel

乔尔

回答by J.G. Hek

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).

对于 EF5 + DbContext 生成器:将 Name.Context.tt 移动到不同的项目很容易。但是,您需要引用模型类。您可以手动执行此操作,但这将要求您在每次生成代码时更改它。您还可以为两个项目使用相同的命名空间。这是有效的并且会起作用,但我认为这是糟糕的设计。另一种替代方法是更改​​ T4 模板 (Name.Context.tt)。

Change this (line 43):

改变这一点(第 43 行):

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
<#
if (container.FunctionImports.Any())
{
#>

To this:

对此:

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
<#
if (modelNamespace != codeNamespace)
#>
using <#=code.EscapeNamespace(modelNamespace)#>;
<#
if (container.FunctionImports.Any())
{
#>

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.

这将检查您的模型命名空间是否与您的代码命名空间不同,如果不同,它将插入所需的 using 来引用您的模型类。