问题描述
我们最近将 Visual Studio 从 2010 年升级到 2013 年.随之而来的是 EF 6.0,它与从 EF 4 生成的代码不兼容.
We have recently upgraded our Visual Studio from 2010 to 2013. With it came EF 6.0, which is incompatible with the code generated from EF 4.
我需要使用数据库架构中的最新版本更新模型中的表.这样做时,删除表定义后的更新"和添加"都会执行以下操作:
I need to update a table in the model with the latest version from the DB schema. When doing that, both "Update" and "Add" after deletion of the table definition do the following:
- 删除已为模型中所有类型编写的所有自定义代码,即使它们与模型位于不同的文件中;
- 生成新的 EF 6.0 绑定;
- (可选)生成新的存储库.现在,这显然与 EF 4 中的行为不同,后者仅重新生成存储库而没有触及自定义代码.
我们没有使用 NuGet,而是在项目中引用了 EF 库.
We are not using NuGet, but referencing the EF library inside the project.
我猜 VS 2013 使用的代码生成器版本与 VS 2010 不同.我们如何恢复到旧行为?
I guess VS 2013 uses a different version of the code generator than VS 2010. How can we revert to the old behavior?
更新:我曾尝试使用 NuGet 下载 EF 4.3.1 并重复更新模型步骤(也删除然后重新创建),但我仍然看到旧行为.我认为 VS 2013 EF 代码生成器有问题.
UPDATE:I have tried using NuGet to download EF 4.3.1 and repeat the update model steps (also delete and then recreate), but I am still seeing the old behavior. I think there is a problem with the VS 2013 EF Code Generator.
推荐答案
对我来说,以下解决方案有效.
For me, the following solution worked.
双击您的 edmx 文件并打开其属性窗口.将 Code Generation Strategy
从 T4
更改为 Legacy Object Context
.构建您的解决方案.它可能会导致数以千计的错误,因为您重新声明了函数和属性,因此请删除之前不存在的所有文件.
Double-click on your edmx file and open its properties window. Change Code Generation Strategy
from T4
to Legacy Object Context
. Build your solution. It may cause thousands of errors because you have redeclared functions and properties, so remove all files that weren't there before.
这篇关于如何在 Visual Studio 2013 中使用实体框架 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!