C# 更新实体框架模型

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

Updating Entity Framework Model

c#visual-studio-2010sql-server-2008entity-frameworklinq-to-entities

提问by Owais Qureshi

I have just started using EF and found it cool, but I ran into a problem,

我刚刚开始使用 EF 并发现它很酷,但我遇到了一个问题,

Problem:
I changed my DB schema of a column inside the table User, It was Varbinary(50) previously I then changed it into VarChar(50), and then inside the MyModel.edmx designer I chose "Update model from database", after clicking finish I received this error.

问题:
我更改了表 User 中一列的数据库架构,之前是 Varbinary(50),然后将其更改为 VarChar(50),然后在 MyModel.edmx 设计器中选择“从数据库更新模型”,之后单击完成我收到此错误。

Error:

错误:

   Error 2019: Member Mapping specified is not valid.
    The type 'Edm.Binary [Nullable=False,DefaultValue=,MaxLength=100,FixedLength=False]' of member
   'Email' in type 'LearnDBModel.User' is not compatible with SqlServer.varchar 
    [Nullable=False,DefaultValue=, MaxLength=50,Unicode=False,FixedLength=False]' of member 'Email'
    in type 'LearnDBModel.Store.User'.

Let me know how to fix it

让我知道如何修复它

采纳答案by Shawn de Wet

I've run into similar issues before, and found that the way to solve it was to delete the table from the model. Save and close the model. Then reopen the model and re-add the table.

之前也遇到过类似的问题,发现解决方法是从模型中删除表。保存并关闭模型。然后重新打开模型并重新添加表。

回答by MSRS

No need to worry about it. Select the affected table in the model. If you observe, there you will find a new column name post fix with an integer (This behavior is only because of the change in the datatype of that column).

无需担心。在模型中选择受影响的表。如果你观察,你会发现一个带有整数的新列名后修复(这种行为只是因为该列的数据类型发生了变化)。

Example if your column name is "Samplecolumn", after updating the model from the database you will get a new column with Samplecolumn1. You can now simply remove the old column "Samplecolumn" and rename the new column "Samplecolumn1" to "Samplecolumn" using the properties window under general category.

例如,如果您的列名称是“Samplecolumn”,则从数据库更新模型后,您将获得一个带有 Samplecolumn1 的新列。您现在可以使用常规类别下的属性窗口简单地删除旧列“Samplecolumn”并将新列“Samplecolumn1”重命名为“Samplecolumn”。

Just build your app. The error will be gone.

只需构建您的应用程序。错误将消失。

回答by Chris Phan

Shawn de Wet's solution works fine but In case you dont want to remove the table (for example relationship to some other tables..) you can use another solution: Open your edmx file with xml Editor, Ctrl + F to find a line similar to

Shawn de Wet 的解决方案工作正常,但如果您不想删除表(例如与其他一些表的关系..),您可以使用另一种解决方案:使用 xml 编辑器打开 edmx 文件,Ctrl + F 找到类似于

Property Name="Email" Type="Binary" Nullable="false" MaxLength="50" FixedLength="false"

属性名称="电子邮件"类型="二进制" Nullable="false" MaxLength="50" FixedLength="false"

Update it to:

将其更新为:

Property Name="Email" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false"

属性名称="电子邮件"类型="字符串" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false"

Save it and rebuild.

保存并重建。

回答by onvr

right click properties from changed table on Model.edmx[Diagram] and "Update model from database" . save and run

右键单击 Model.edmx[Diagram] 上已更改表的属性和“从数据库更新模型”。保存并运行

回答by Tomas Kubes

A lot of files in the EF model get f*****d. Removing and adding the entity was not enough. The entities was duplicated like table, table1, table_result, table1_result, table_result1, and so on... Model update was updating the duplicated references instead of the original.

EF 模型中的很多文件都得到了 f*****d。删除和添加实体是不够的。实体被复制,如 table、table1、table_result、table1_result、table_result1 等等......模型更新正在更新重复的引用而不是原始引用。

I have to open notepad and fix manually these files:

我必须打开记事本并手动修复这些文件:

EFModel.Context.cs
EFModel.edxm

And delete these files:

并删除这些文件:

obj\Debug\edmxResourcesToEmbed\MYEfModel.csdl
obj\Debug\edmxResourcesToEmbed\MYEfModel.msl
obj\Debug\edmxResourcesToEmbed\MYEfModel.ssdl

回答by HoganDevelopementAddicted

go to MyModel.edmx xml file, change Binary to String solved my issue

转到 MyModel.edmx xml 文件,将 Binary 更改为 String 解决了我的问题