Entity Framework: “更新向导”不会添加表

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

这是一个 bug,还是在某些情况下表不能(通过设计)添加到模型中?


更新: 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>-->
63645 次浏览

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.

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.

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)

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

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

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

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

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

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.

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.

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.

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.

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.

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.

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.

hope the extra detail helps somebody.

Joe

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.

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.

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...

One easy way I found was to (in SQL Server Management Studio) add an ID column of type INT, deselect "Allow Nulls", and under Column properties (of ID) >> Identity Specification >> check (Is Identity), and make sure increment is set.

Once the above is done, go back to your .edmx window, right-click, and Update Model from Database.

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.

Tables without primary keys will not be added.

You can add a Primary Key column cross table. Then problem is resolving

One other solution is to ensure the account you are accessing the database via has some permissions to the table, Select at least. The table won't be added if the account has no permissions to it.

In SSMS under Security, right click the user or role, then Properties. Then click on Securables. The top table shows tables currently accessible. If the missing tables aren't there, click on search and find then, highlight them in the top table, then assign permissions in the lower table