C# 将列添加到数据库表后实体框架出现问题

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

Problem with Entity Framework after adding column to DB table

c#asp.netsql-serverentity-framework

提问by Marcus L

I needed to add a new NVARCHAR column to a table in my DB. So I added the column, and then fired up Visual Studio to update the EDMX-file for the Entity Framework.

我需要向我的数据库中的表添加一个新的 NVARCHAR 列。所以我添加了该列,然后启动 Visual Studio 来更新实体框架的 EDMX 文件。

I ran update model from database on everything, which only resulted in "data reader is incompatible"-errors. So I renamed the whole table in the DB, updated EDMX from database, renamed the table back to the original name, ran update again, and then created new function imports for all affected stored procedures. But I still get the same error:

我对所有内容从数据库运行更新模型,这只会导致“数据读取器不兼容”-错误。所以我重命名了数据库中的整个表,从数据库更新了 EDMX,将表重命名回原始名称,再次运行更新,然后为所有受影响的存储过程创建新的函数导入。但我仍然得到同样的错误:

The data reader is incompatible with the specified '[Model].[Entity]'. A member of the type, '[Column]', does not have a corresponding column in the data reader with the same name.

数据读取器与指定的“[Model].[Entity]”不兼容。'[Column]' 类型的成员在数据读取器中没有对应的同名列。

I've looked around a bit, and this seems to be a common error if the column name is different in the database and framework. This is however not the case, they have the same name.

我环顾四周,如果数据库和框架中的列名不同,这似乎是一个常见错误。然而事实并非如此,它们具有相同的名称。

I can access the column in the code via [Entity].Context.[Column], so I don't quite see what the data reader is complaining about.

我可以通过 [Entity].Context.[Column] 访问代码中的列,所以我不太明白数据阅读器在抱怨什么。

I've run out of ideas, so any help welcome.

我的想法已经用完了,所以欢迎任何帮助。

采纳答案by Marcus L

Turns out the EDMX was fine, but the designer has, for some odd reason, stopped update Designer.cs in my project.

结果证明 EDMX 很好,但设计师出于某种奇怪的原因停止了我项目中的 Designer.cs 更新。

Had to go in and manually edit it.

不得不进去手动编辑它。

回答by Craig Stuntz

Updating the model replaces the store schema, but not the client schema or the mapping. To start with "a clean slate", back up your current EDMX, then open it as XML. Remove all references to the table, then close and open in the graphical error. Build. If you have any errors (perhaps broken links to the deleted table), fix them. Then update model to re-add the table.

更新模型会替换商店架构,但不会替换客户端架构或映射。要从“一尘不染”开始,请备份您当前的 EDMX,然后将其作为 XML 打开。删除对表的所有引用,然后关闭并在图形错误中打开。建造。如果您有任何错误(可能是已删除表的断开链接),请修复它们。然后更新模型以重新添加表。

回答by abend

For me it was an issue where I altered an entity (vertical entity splitting), but one of my stored procedures wasn't pulling data for that new field. I added it (in the SP) and now everything runs fine.

对我来说,我更改了一个实体(垂直实体拆分)是一个问题,但是我的一个存储过程没有为该新字段提取数据。我添加了它(在 SP 中),现在一切正常。

回答by Brian

Just had the same issues as above. Tried removing the entity from the edmx, tried removing and re-adding the function import, and finally rebuilt using a complex type in the function import and that didn't work either.

刚刚遇到了和上面一样的问题。尝试从 edmx 中删除实体,尝试删除并重新添加函数导入,最后在函数导入中使用复杂类型重建,但也不起作用。

I realize this error can occur for really any scenario where there is a mismatch but we found the issue was in the stored proc we used for the function import. The stored proc was using specific select columnname, columnname etc and the new column we added to the table was not in that list. To test, we used the *, updated the EDMX and it resolved our issue.

我意识到这个错误可能发生在任何不匹配的场景中,但我们发现问题出在我们用于函数导入的存储过程中。存储过程使用特定的选择列名、列名等,我们添加到表中的新列不在该列表中。为了测试,我们使用了 *,更新了 EDMX 并解决了我们的问题。

回答by Dan B

Unless it's simply adding a new item, This throws an error just about every time I try to "Update model from database" ...

除非它只是添加一个新项目,否则每次我尝试“从数据库更新模型”时都会引发错误......

The solution is luckily very simple -

幸运的是,解决方案非常简单 -

1) Open Web.config, find <connectionStrings> node 
2) Delete the DBNameEntities <connectionStrings>  
     - this way you do not have to modify any references to your emdx model
2) Delete the ADO.NET Entity Data Model (.emdx) 
3) Re-add the ADO.NET Entity Data Model (.emdx) with the same name.

Its faster and bulletproof. (so far!?)

它更快且防弹。(迄今为止!?)

回答by Finesse74

If you Re-Add the DataModel you will lose all DataBindings - especially bindings of controls on your form (like DbGrid). I have fixed this issue manually editing the DataModel.edmx file in external Editor.

如果您重新添加数据模型,您将丢失所有数据绑定 - 特别是表单上的控件绑定(如 DbGrid)。我已经修复了这个问题,在外部编辑器中手动编辑 DataModel.edmx 文件。