ADO.NET Entity Framework:更新向导不会添加表

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

ADO.NET Entity Framework: Update Wizard will not add tables

.netvisual-studio-2008entity-frameworkado.netprimary-key

提问by Robert Claypool

I added a new ADO.Net Entity Data Modelinto my project and used the Update Wizardto add tables into the model. Five of the selected tables were added to the design surface. Two other tables will not add. I select them in the wizard and click Finish, yet they never show up on the design surface.

我在我的项目中添加了一个新的 ADO.Net 实体数据模型,并使用更新向导将表添加到模型中。五个选定的表已添加到设计图面。另外两个表不会相加。我在向导中选择它们并单击“完成”,但它们从未出现在设计图面上。

Is this a bug, or are there some situations where a table cannot be added to the model (by design)?

这是一个错误,还是在某些情况下无法将表添加到模型中(按设计)?



UPDATE: The XML (*.edmx) reveals the problem.

更新:XML (*.edmx) 揭示了这个问题。

<!--Errors Found During Generation:
warning 6013: The table/view 'FooBar.dbo.Roles' does not 
have a primary key defined and no valid primary key could be inferred. 
This table/view has been excluded. To use the entity you will need to 
review your schema, add the correct keys and uncomment it.-->
<!--<EntityType Name="Roles">
    <Property Name="role_id" Type="decimal" />
    <Property Name="role_desc" Type="nvarchar" MaxLength="30" />
</EntityType>-->

采纳答案by Craig Stuntz

The design surface is different from the entity model. It is possible to have a table in the mapping in your EDMX which does not appear on the design surface. View the file as XML to see if this is the case. In this case, the Update Wizard will not allow you to re-add the tables, since they are already part of the entity model. So, generally speaking, the Update Wizard knows more about your entity model than it does about the design surface, per se.

设计图面不同于实体模型。在您的 EDMX 中的映射中可能有一个没有出现在设计图面上的表。以 XML 格式查看文件以查看是否是这种情况。在这种情况下,更新向导将不允许您重新添加表,因为它们已经是实体模型的一部分。因此,一般来说,更新向导对实体模型的了解比对设计图面本身的了解要多

I don't think that's exactly the situation you're in, but it should give you the general idea for the solution: go into the XML and look for references to the tables in question.

我不认为这正是您所处的情况,但它应该为您提供解决方案的总体思路:进入 XML 并查找对相关表的引用。

回答by Parvinder

Set Primary Keys to all tables or just one unchecked check-box "Allow null" to any column of each table. It works for me :)

将主键设置为所有表,或者将一个未选中的复选框“允许为空”设置为每个表的任何列。这个对我有用 :)

回答by Pratap

1.Change the Table structure and add a Primary Column. Update the Model.

1.更改表结构并添加主列。更新模型。

2.Modify the .EDMX file in XML Editor and try adding a New Column under tag for this specific table. (WILL NOT WORK)

2.在 XML 编辑器中修改 .EDMX 文件并尝试在此特定表的标记下添加一个新列。(不管用)

3.Instead of creating a new Primary Column to Exiting table, i will make a composite key by involving all the existing columns.(WORKED)

3.不是为退出表创建一个新的主列,我将通过涉及所有现有列来制作一个复合键。(工作)

Entity Framework: Adding DataTable with no Primary Key to Entity Model.

实体框架:将没有主键的数据表添加到实体模型。

回答by Kip ei

I had this problem too, all the above didn't work for me. What helped for me was the following.

我也有这个问题,以上所有对我都不起作用。对我有帮助的是以下内容。

When you try to connect with a database that database can have different users with different credentials it can accept. Let's say user A till D.

当您尝试连接一个数据库时,该数据库可以拥有具有不同凭据的不同用户,它可以接受。假设用户 A 到 D。

If you try to connect with a user make sure that user has the right credentials enabled, in this case, read and write options enabled.

如果您尝试与用户连接,请确保该用户启用了正确的凭据,在这种情况下,启用了读取和写入选项。

To do this start MS SQL Server Managment Studio, connected with your SQL server and select the database you try to make a connection with in visual studio. Under 'your_dbname' --> Security --> Users you find a list of users. Rightclick the username you try to login with and select properties. A window opens. Select the 'General' (selected by default) page and under tab 'Database role membership' make sure 'db_datareader' and 'db_datawrite' are selected.

为此,请启动 MS SQL Server Management Studio,连接到您的 SQL 服务器并选择您尝试在 Visual Studio 中建立连接的数据库。在“your_dbname”--> 安全--> 用户下,您可以找到用户列表。右键单击您尝试登录的用户名并选择属性。一个窗口打开。选择“常规”(默认选择)页面,并在“数据库角色成员资格”选项卡下确保选择“db_datareader”和“db_datawrite”。

Note: When you log in too MS SQL Server Managment Studio make sure you log in with a user which can enable/disable these options...

注意:当您也登录 MS SQL Server Management Studio 时,请确保您使用可以启用/禁用这些选项的用户登录...

回答by Ketan Dubey

Check for 'Nulls' in your particular table. If all the columns are set to 'Allow Null' Entity Framework considers it as a 'Null' table. I had the same problem and unchecked 'Allow Null' and it added the required table.

检查特定表中的“空”。如果所有列都设置为“允许空”实体框架将其视为“空”表。我遇到了同样的问题,未选中“允许空”并添加了所需的表。

回答by user8155123

Tables without primary keys will not be added.

不会添加没有主键的表。

回答by user8155123

My solution to this is to delete the entire model and readd it including the new tables that I want.

我对此的解决方案是删除整个模型并读取它,包括我想要的新表。

I accidentally deleted a table in the designer once and could not readd it. Hence removing entire model and readding it.

有一次我不小心删除了设计器中的一个表,无法读取。因此删除整个模型并阅读它。

I had some issues with this as well (Ado.Net Entity Data Model Not Updating Correctly)

我也遇到了一些问题(Ado.Net 实体数据模型未正确更新

回答by Gyaneshwar Rao N

This has been resolved in latest version(environment: VS 2012 , .net framework 4.5). Simple open the .edmx file and add the required tables / stored procedures / views. The table/view which doesnt have primary key will be created as read only table/view.

这已在最新版本中得到解决(环境:VS 2012,.net framework 4.5)。简单地打开 .edmx 文件并添加所需的表/存储过程/视图。没有主键的表/视图将创建为只读表/视图。

The table/view 'TABLE_NAME' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.

表/视图“TABLE_NAME”没有定义主键。已推断出键并将定义创建为只读表/视图。

回答by RoyGr

You do not need to delete the entire model (that might be hundreds of tables!).

您不需要删除整个模型(可能有数百个表!)。

The Wizard can handle "adding new" items, but some changes such as changing composite keys and renaming columns - it doesn't know what to do - so it doesn't try - and things get out of sync.

向导可以处理“添加新”项目,但是一些更改,例如更改组合键和重命名列 - 它不知道要做什么 - 所以它不会尝试 - 并且事情会变得不同步。

SO: DELETE the tables in question from the Model (conceptual mode) AND from the Model.Store, and then Update the model using the Wizard, and re-add just the tables in question.

SO:从模型(概念模式)和 Model.Store 中删除有问题的表,然后使用向导更新模型,并仅重新添加有问题的表。

回答by Joe Ruder

Just throwing out some more detail for anybody who has not had to do this before. I am using visual studio 2013 with update 4 installed. I added some fields to a table, deleted the table from my model then went to update and it would not let me check any of the tables.

只是为以前不必这样做的人提供更多细节。我正在使用安装了更新 4 的 Visual Studio 2013。我在表中添加了一些字段,从我的模型中删除了表,然后去更新,它不会让我检查任何表。

I do have a primary key.

我确实有一个主键。

I created a new test table, that showed up and it would let me check it but not my original.

我创建了一个新的测试表,它出现了,它可以让我检查它而不是我的原始表。

I have never opened it in XML before and did not know how to do that. You right click on the .edmx file and "open with" -- choose the xml editor.

我以前从未在 XML 中打开过它,也不知道该怎么做。您右键单击 .edmx 文件并“打开方式”——选择 xml 编辑器。

the table in question was there all right, it even had the new fields (strange).

有问题的表就在那里,它甚至有新的字段(奇怪)。

I deleted all the references to it (took a couple times) -- after you delete one and save, if you try to open it and it won't display just go ahead and choose "view XML" - look at the errors and basically follow the bouncing ball until you have cleaned it up.

我删除了对它的所有引用(花了几次)-删除一个并保存后,如果您尝试打开它并且它不会显示,请继续并选择“查看 XML”-查看错误,基本上跟随弹跳球直到你把它清理干净。

hope the extra detail helps somebody.

希望额外的细节可以帮助某人。

Joe