93
votes

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>-->
13
This should have WAY more vote ups. I had no idea Visual Studio couldn't add the table since the .edmx file is the only place the error actually goes. Hopefully they'll have a dialog box in v2!John B
+1 - In my case, there was no error message.teynon
As you noted, the tables that wouldn't import didn't have primary keys. Add a PK and you're good to go.Eric

13 Answers

46
votes

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.

43
votes

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

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

6
votes

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

5
votes

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.

3
votes

Tables without primary keys will not be added.

1
votes

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)

1
votes

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.

0
votes

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.

0
votes

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

0
votes

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.

0
votes

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

0
votes

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