C# 从 T4 更改 EF 6 代码生成策略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19380820/
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-08-10 14:51:01  来源:igfitidea点击:

Change EF 6 Code Generation Strategy from T4

c#entity-framework

提问by Family

In the past I have successfully created edmx files. This was using EF5 and Visual Studio 2012. I have since upgraded to Visual Studio 2013 and EF6. The existing EF 5 code still works, but now I have a problem. I can create edmx files, and an EntityDataSource, I can configure the data source, it sees the tables and columns fine, but when I try to refresh the schema I get:

过去我已经成功创建了 edmx 文件。这是使用 EF5 和 Visual Studio 2012。我已经升级到 Visual Studio 2013 和 EF6。现有的 EF 5 代码仍然有效,但现在我遇到了问题。我可以创建 edmx 文件和 EntityDataSource,我可以配置数据源,它可以很好地查看表和列,但是当我尝试刷新架构时,我得到:

The schema could not be determined because of the following error from the EntityDataSource:

Could not find the CLR type for (my type here)

由于来自 EntityDataSource 的以下错误,无法确定架构:

找不到(我的类型在这里)的 CLR 类型

I have seen a solution on-line telling me to change my Code Generation Strategy to default (the existing edmx files created in EF 5 are set as Legacy ObjectContext), but it won't let me do this because the option 'T4' is grayed out.

我在网上看到了一个解决方案,告诉我将代码生成策略更改为默认值(在 EF 5 中创建的现有 edmx 文件设置为 Legacy ObjectContext),但它不会让我这样做,因为选项“T4”是变灰。

enter image description here

在此处输入图片说明

Is there a way I can force the Code Generation Strategy to not use T4?

有没有办法强制代码生成策略不使用 T4?

EDIT:

编辑:

Pawel states that EntityDataSource does not support EF 6. Is there a tutorial available that shows an easy step-by-step guide of how to connect to EF 6? I have got all my EF information from the book Beginning ASP.Net 4, but it is now obviously out-dated. I see that EF 6 is still in beta stage. Maybe they'll add support for EntityDataSource at some stage?

Pawel 指出 EntityDataSource 不支持 EF 6。是否有可用的教程显示如何连接到 EF 6 的简单分步指南?我从 Beginning ASP.Net 4 一书中获得了我所有的 EF 信息,但它现在显然已经过时了。我看到 EF 6 仍处于测试阶段。也许他们会在某个阶段添加对 EntityDataSource 的支持?

EDIT 2:

编辑2:

OK, I've been fiddling around with this for a bit, and I can connect using LinqDataSource. I won't get a chance to play around with it much for a few days, but it looks like this works.

好的,我一直在摆弄这个,我可以使用 LinqDataSource 进行连接。几天后我将没有机会使用它,但它看起来很有效。

EDIT 3:

编辑 3:

Using LinqDataSource doesn't work. The only CRUD operation it can perform is Read. Obviously there must be a way to use the new EF 6.0 framework (Pawel has suggested I use MVC), but they couldn't have broken it completely for my scenario (using web site), could they?

使用 LinqDataSource 不起作用。它可以执行的唯一 CRUD 操作是读取。显然,必须有一种方法可以使用新的 EF 6.0 框架(Pawel 建议我使用 MVC),但是对于我的场景(使用网站),他们不可能完全破坏它,是吗?

EDIT 4:

编辑 4:

I have found a solution for my scenario, see my answer below.

我找到了适合我的方案的解决方案,请参阅下面的答案。

采纳答案by Pawel

Due to changes in EF6 if you are using EF6 in VS2012/VS2013 the only supported generation strategy is T4. You can get EntityObject based entities and ObjectContext based context by using T4 templates from VS Gallery. Note: EntityDataSource does not currently support EF6.

由于 EF6 的变化,如果您在 VS2012/VS2013 中使用 EF6,唯一支持的生成策略是 T4。您可以使用VS Gallery 中的 T4 模板获取基于 EntityObject 的实体和基于 ObjectContext 的上下文。注意:EntityDataSource 目前不支持 EF6。

EDIT

编辑

The preview of the EntityDataSourceControl with EF6 support is now available

现已提供支持 EF6 的 EntityDataSourceControl 预览版

回答by Family

As Pawel mentions you can only use T4 in EF 6. What I did to get my system back to using EF 5 was to uninstall EF 6, using the NuGet package manager (Website -> Manage NuGet Packages -> Installed Packages -> EntityFramework -> Uninstall). Then I had to add EF 5 using NuGet package manager (same menu but Online instead of Installed Packages). Then I had to remove the following from web.config:

正如 Pawel 提到的,您只能在 EF 6 中使用 T4。为了让我的系统恢复使用 EF 5,我所做的是使用 NuGet 包管理器卸载 EF 6(网站 -> 管理 NuGet 包 -> 已安装的包 -> EntityFramework - > 卸载)。然后我不得不使用 NuGet 包管理器添加 EF 5(相同的菜单,但在线而不是已安装的包)。然后我不得不从 web.config 中删除以下内容:

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

Now when I add an ADO.NET Entity Data Model it asks me what version of Entity Framework I want to use, but Entity Framework 6.0 is grayed out. This is obviously a bug, it should ask this when EF 6.0 is installed, not when it isn't. When it was installed it never asked what version I wanted and just automatically created an edmx for version 6.0

现在,当我添加 ADO.NET 实体数据模型时,它会询问我要使用哪个版本的实体框架,但实体框架 6.0 呈灰色显示。这显然是一个错误,它应该在安装 EF 6.0 时询问,而不是在未安装时询问。安装时它从不询问我想要什么版本,只是自动为 6.0 版创建了一个 edmx

回答by ransems

I have the same issue and (EF6) and when I changed the Metadata Artifact Processing to "Copy to Output Directory" I still get the same error. Which has been suggested as a solution to most of my problems.

我有同样的问题和(EF6),当我将元数据工件处理更改为“复制到输出目录”时,我仍然遇到相同的错误。这已被建议作为我大多数问题的解决方案。

My steps (to reproduce error):

我的步骤(重现错误):

Create a directory called DAL, add edmx file, allow it to create connection string, add two tables, build.

创建一个名为 DAL 的目录,添加 edmx 文件,允许它创建连接字符串,添加两个表,构建。

Create a default.aspx page, drag the EntityDataSource from toolbox, click the flyout Configure DataSource, choose my named Connection from above, and I get the error:

创建一个 default.aspx 页面,从工具箱中拖动 EntityDataSource,单击弹出的配置数据源,从上面选择我命名的连接,然后出现错误:

The metatdata specified in the connection string could not be loaded. Consider rebuilding the web project to build assemblies that may contain metadata. The following error(s) occurred: The provider did not return a ProviderManifest instance.

无法加载连接字符串中指定的元数据。考虑重新构建 Web 项目以构建可能包含元数据的程序集。发生以下错误:提供程序未返回 ProviderManifest 实例。

Been trying to find a solution for about 24h now.

现在一直试图找到大约 24 小时的解决方案。

So now when I choose EF5 (not 6) and I do steps above I right click on the Diagram Surface and go to properties I change two setting:

所以现在当我选择 EF5(不是 6)并执行上述步骤时,我右键单击图表表面并转到属性,我更改了两个设置:

1.) Artifact Processing to "Copy to Output Directory" 2.) Change the Code Generation Strategies to "Legacy ObjectContext" from T4

1.) 工件处理到“复制到输出目录” 2.) 将代码生成策略从 T4 更改为“Legacy ObjectContext”

Which allows me to drag the EntityDataSource from the toolbox, click the flyout Configure DataSource, choose my named Connection from above and everything is back to world of love an programming...

这允许我从工具箱中拖动 EntityDataSource,单击弹出的配置数据源,从上面选择我命名的连接,一切都回到了编程的世界......

Only took me 24 solid hours and 2 wasted days of coding to finally piece together a suitable solution. And my laptop got flatten and reinstalled, b/c I was soooo desperate and pulling my hair out.

我只花了 24 个小时的时间和 2 天浪费的编码时间,才最终拼凑出一个合适的解决方案。我的笔记本电脑变平并重新安装,b/c 我太绝望了,把头发拔了出来。

回答by Amarjit Singh Virkh

In Ef6 you can use legacy context. Just right click the edmx file and in open with option open it in XML viewer. Then search for legacy and set the value of it as true.

在 Ef6 中,您可以使用遗留上下文。只需右键单击 edmx 文件并在打开选项中在 XML 查看器中打开它。然后搜索 legacy 并将其值设置为 true。

回答by lmoglia

i already have VS2013 Community, and i was fighting with the same problem. Just like your image, the code generation strategy was disabled, i mean, it wasn't possible to change, but... right click on the property name "Code Generation Strategy" then just click 'Reset' and the property value will change to Legacy ObjectContext!

我已经有了 VS2013 社区,我也遇到了同样的问题。就像您的图片一样,代码生成策略已禁用,我的意思是,无法更改,但是...右键单击属性名称“代码生成策略”,然后单击“重置”,属性值将更改到旧的 ObjectContext!

I hope that it can help someone!

我希望它可以帮助某人!

PD:Sorry, my english is aweful! xP

PD:对不起,我的英语太棒了!xP

回答by Jaiyaram Mahendran

I am using visual studio 2015

我正在使用 Visual Studio 2015

  1. Dbl click the xxx.edmx file in the solution explorer, this should open the diagarm in the designer,

  2. press alt+enter which opens the property window

  3. Look for "Code Generation Strategy" property - choose the value T4 from the drop down to Legacy Object Content.

  1. Dbl 单击解决方案资源管理器中的 xxx.edmx 文件,这应该会在设计器中打开图表,

  2. 按 alt+enter 打开属性窗口

  3. 查找“代码生成策略”属性 - 从下拉列表中选择值 T4 到 Legacy Object Content。