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

.NetVisual Studio-2008Entity Frameworkado.netPrimary Key

.Net Problem Overview


I added a new ADO.Net Entity Data Model into my project and used the Update Wizard to 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.

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.

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

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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

Solution 3 - .Net

  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.

Solution 4 - .Net

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

Solution 5 - .Net

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.

Solution 6 - .Net

Tables without primary keys will not be added.

Solution 7 - .Net

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 (https://stackoverflow.com/questions/774645/ado-net-entity-data-model-not-updating-correctly/774737#774737)

Solution 8 - .Net

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.

Solution 9 - .Net

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.

Solution 10 - .Net

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

Solution 11 - .Net

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.

Solution 12 - .Net

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

Solution 13 - .Net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobert ClaypoolView Question on Stackoverflow
Solution 1 - .NetCraig StuntzView Answer on Stackoverflow
Solution 2 - .NetParvinderView Answer on Stackoverflow
Solution 3 - .NetPratapView Answer on Stackoverflow
Solution 4 - .NetKip eiView Answer on Stackoverflow
Solution 5 - .NetKetan DubeyView Answer on Stackoverflow
Solution 6 - .NetYongaJView Answer on Stackoverflow
Solution 7 - .NetGarthView Answer on Stackoverflow
Solution 8 - .NetGyaneshwar Rao NView Answer on Stackoverflow
Solution 9 - .NetRoyGrView Answer on Stackoverflow
Solution 10 - .NetJoe RuderView Answer on Stackoverflow
Solution 11 - .NetRyan DView Answer on Stackoverflow
Solution 12 - .NetMehmet KurtView Answer on Stackoverflow
Solution 13 - .NetMark WorrallView Answer on Stackoverflow