实体框架5和Visual Studio 2012 POCO类在不同的项目 [英] Entity Framework 5 and Visual Studio 2012 POCO Classes in Different Project

查看:114
本文介绍了实体框架5和Visual Studio 2012 POCO类在不同的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS 2010和EF 4.4中,您可以在Entity Framework中使用DBContext生成器时移动和编辑.tt文件,以使您的POCO对象与DBContext文件不同的项目。



请参阅这里此处我正在谈论的例子。



在VS2012 / EF5这似乎是不可能的。 POCO类将生成为EDMX文件下的子项。这些文件无法从Visual Studio中复制。从资源管理器移动文件不会有帮助,因为您在移动的文件在编译时重新创建。



我在这里缺少一些基本的东西?



我没有使用EF5的任何代码生成项目(而我使用的是EF4.x.)这可能是有区别的吗?

解决方案

在Visual Studio 2012中,当您添加ADO.NET实体数据模型(* .edmx)时,它将上下文和模型类的T4模板作为EDMX文件的子项。例如,如果添加了MyModel.edmx,它将具有以下4个子项:


  1. MyModel.Context.tt

  2. MyModel.Designer.cs(在C#项目中)

  3. MyModel.edmx.diagram

  4. MyModel.tt

MyModel.tt生成作为子项目的POCO实体。要在单独的项目中生成实体,请执行以下步骤:


  1. 创建一个单独的课程项目。

  2. 添加新项目,选择EF 5.x DbContext Generator模板。这将创建* .tt文件。例如MyModel.tt。

  3. 编辑模板文件如下:

      const string inputFile = @MyModel.edmx; // old value(remove)
    const string inputFile = @.. \MyOtherProjectName\MyModel.edmx; //新值


  4. 在其他项目中,展开EDMX文件,然后右键单击MyModel.tt ,请选择删除。

就是这样。你完成了。您现在可以在一个项目中将您的模型和上下文放在一个单独的项目中。


In VS 2010 and EF 4.4, you were able to move and edit .tt files when using the DBContext generator in Entity Framework such that your POCO objects where in a different project than your DBContext files.

See Here and Here for examples of what I am talking about.

In VS2012 / EF5 this seems not to be possible. the POCO classes are generated as a subitem under the EDMX file. The files cannot be copied from within Visual Studio. Moving the files from Explorer does not help because the files you moved get recreated at compilation time.

Am I missing something basic here?

I am not using any code generation items with EF5 (whereas I was with EF4.x.) Could that be the difference?

解决方案

In Visual Studio 2012, when you add an ADO.NET Entity Data Model (*.edmx), it includes the T4 templates for the context and model classes as sub-items of the EDMX file. For example, if you add MyModel.edmx, it will have 4 sub-items as follows:

  1. MyModel.Context.tt
  2. MyModel.Designer.cs (in C# projects)
  3. MyModel.edmx.diagram
  4. MyModel.tt

MyModel.tt generates the POCO entities as sub-items. To generate the entities in a separate project, follow the following steps:

  1. Create a separate class project.
  2. Add new item, choose the "EF 5.x DbContext Generator" template. This creates the *.tt file. For example MyModel.tt.
  3. Edit the template file as follows:

    const string inputFile = @"MyModel.edmx"; // old value (remove)
    const string inputFile = @"..\MyOtherProjectName\MyModel.edmx"; // new value
    

  4. In your other project, expand the EDMX file and right-click on MyModel.tt, select Delete.

That's it. You're done. You now have your model and context in one project and the entities in a separate project.

这篇关于实体框架5和Visual Studio 2012 POCO类在不同的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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