C# 更新实体框架模型后,Visual Studio 看不到变化

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

After updating Entity Framework model, Visual Studio does not see changes

c#entity-frameworkentity-framework-5

提问by Kyle

If I do any changes to my EF 5.0 model, VS does not seem to see the changes. I have tried adding a new table, which shows up fine in the model, but then if I try to use it somewhere the table does not show up in intellisense and I can't use it.

如果我对 EF 5.0 模型进行任何更改,VS 似乎看不到这些更改。我尝试添加一个新表,它在模型中显示良好,但是如果我尝试在某处使用它,该表不会显示在智能感知中,我无法使用它。

I have also tried changing datatypes of a column, which again shows up fine if I look at the .edmx model file, but visual studio will still give me compiler errors on that field because I am trying to assign the incorrect datatype.

我还尝试更改列的数据类型,如果我查看 .edmx 模型文件,这再次显示正常,但 Visual Studio 仍会在该字段上给我编译器错误,因为我试图分配不正确的数据类型。

If I delete the EDMX completely, and recreate it, then the changes work fine. but it is tiresome to regenerate from database each time when I should be able to just "update model from database" and rebuild.

如果我完全删除 EDMX,然后重新创建它,则更改工作正常。但是每次当我应该能够“从数据库更新模型”并重建时,从数据库中重新生成是很烦人的。

I have never had these problems with EF before. Does anyone know the cause or the fix to this problem?

我以前从未在 EF 上遇到过这些问题。有谁知道这个问题的原因或解决方法?

Thanks.

谢谢。

采纳答案by Pawel

If this is the bug with the edmx file located in a folder it is now fixed - download and install VS 2012 Update 1. You can get it from: http://www.microsoft.com/visualstudio/eng/downloads#d-visual-studio-2012-update

如果这是位于文件夹中的 edmx 文件的错误,现在已修复 - 下载并安装 VS 2012 Update 1。您可以从:http: //www.microsoft.com/visualstudio/eng/downloads#d- visual-studio-2012-update

回答by Middas

Are you working in an N-Tiered project? If so, try rebuilding your Data Layer (or wherever your EDMX file is stored) before using it.

您是否在 N 层项目中工作?如果是这样,请在使用之前尝试重建数据层(或存储 EDMX 文件的任何位置)。

回答by Nicholas Butler

You should have a <XXX>Model.ttfile somewhere which is the T4 template that generates your model classes.

您应该在<XXX>Model.tt某处有一个文件,它是生成模型类的 T4 模板。

If it is in a different project, it will not update when you save the edmx file.

如果它在不同的项目中,则在保存 edmx 文件时它不会更新。

Anyway, try right-clicking on it in Solution Explorer and choosing Run Custom Tool

无论如何,尝试在解决方案资源管理器中右键单击它并选择 Run Custom Tool

回答by Ghlouw

This is apparently a bug in the Entity Framework that the model does not get updated when your Edmx file is located inside a folder. The workarounds available at the moment are:

这显然是实体框架中的一个错误,即当您的 Edmx 文件位于文件夹内时,模型不会更新。目前可用的解决方法是:

  1. Install VS 2012 Update 1 which should fix the bug.
  2. If you are not in a position to install Update 1, you will have to right click on the model.tt T4 template file and click run custom tool. This will update the classes for you.
  1. 安装 VS 2012 Update 1 应该可以修复这个错误。
  2. 如果您无法安装 Update 1,则必须右键单击 model.tt T4 模板文件,然后单击运行自定义工具。这将为您更新课程。

Hope that helps someone out there.

希望能帮助那里的人。

Link: http://thedatafarm.com/blog/data-access/watch-out-for-vs2012-edmx-code-generation-special-case/

链接:http: //thedatafarm.com/blog/data-access/watch-out-for-vs2012-edmx-code-generation-special-case/

回答by Mahdi Shahbazi

First Build your Project and if it was successful, right click on the "model.tt"file and choose run custom tool. It will fix it.

首先构建您的项目,如果成功,请右键单击“model.tt”文件并选择运行自定义工具。它会修复它。

Again Build your project and point to "model.context.tt"run custom tool. it will update DbSetlists.

再次构建您的项目并指向“model.context.tt”运行自定义工具。它将更新DbSet列表。

回答by Kristian

I also had this problem, however, right-clicking on the model.tt file and running "Custom tool" didn't make any difference for me somehow, but a comment on the page Ghlouw linked to mentioned to use the menu item "BUILD > Transform All T4 Templates." which did it for me

我也遇到了这个问题,但是,右键单击 model.tt 文件并运行“自定义工具”对我没有任何影响,但是 Ghlouw 链接到的页面上的评论提到使用菜单项“构建> 转换所有 T4 模板。” 这是为我做的

回答by woodvi

I searched for this answer because I had a similar situation in VS2013. In my case, I found that a simple "Clean Solution" cleared out all the old definitions.

我搜索这个答案是因为我在 VS2013 中也有类似的情况。就我而言,我发现一个简单的“ Clean Solution”清除了所有旧定义。

回答by CF5

I've also experienced this problem with none of the classes being generated under the model.tt file. In my case it was down to issues with how I had built the DB in SQL2012. I'd set a column in a table to nullable that was also a foreign key and although I think you should be able to do this it caused a problem in EF5.

我也遇到过这个问题,没有在 model.tt 文件下生成任何类。就我而言,这归结为我如何在 SQL2012 中构建数据库的问题。我将表中的一列设置为可为空的,这也是一个外键,尽管我认为您应该能够做到这一点,但它在 EF5 中引起了问题。

As soon as this was cleared and the diagram updated from the database they reappeared.

一旦清除并从数据库更新图表,它们就会重新出现。

EF5 VS2013

EF5 VS2013

回答by Tshilidzi Mudau

Right click the .ttfile and select "Run Custom Tool", that should update it: see image

右键单击该.tt文件并选择“运行自定义工具”,这将更新它: 看图片