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

13

93

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>-->
Bard answered 12/2, 2009 at 23:18 Comment(3)
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!Glottal
+1 - In my case, there was no error message.Danidania
As you noted, the tables that wouldn't import didn't have primary keys. Add a PK and you're good to go.Terceira
F
46

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.

Faunia answered 13/2, 2009 at 14:16 Comment(1)
I've just had this very problem, I deleted a table from the model and when I went to add it again the visual designer didn't show it as being in the database. To get round it I deleted the text from the .edmx file, but managed to mess up the XML. If you do this please back the file up first, just in case. Luckily, I was able to find the messed up tag with git diff.Geosynclinal
S
45

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

Sherbrooke answered 21/7, 2010 at 11:37 Comment(3)
This I believe is the correct answer, at least it was for me. Thanks!Terceira
@Parvinder, I have struggled about this issue in most of my project. Eventually, Your answer made it all clear for me. In my case the, In Joining table I didn't specified the Not null key field or just primary key. After fixing this, I had a peace of mind. Thanks a lotRodmun
What I understood with the resolution is that we can't add a table into Entity Model if i) that table does not have a primary key or (ii) There's no Allow Null column in that table.Binoculars
O
14
  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.

Odie answered 29/4, 2012 at 18:9 Comment(1)
I ran into this same issue. The Entity Model was adding the table, however, it was not showing in the designer. I also could not access it in code. I added an ID field Key... worked!Battledore
M
6

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

Manganese answered 13/1, 2015 at 12:54 Comment(0)
S
6

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.

Schreiner answered 1/3, 2017 at 20:58 Comment(1)
It works, stupid no error nor warning appears in the logs!Pvc
J
4

Tables without primary keys will not be added.

Jovial answered 5/4, 2019 at 11:48 Comment(0)
E
1

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)

Eudemonia answered 21/4, 2009 at 21:37 Comment(0)
L
1

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.

Leith answered 18/4, 2013 at 3:22 Comment(1)
the error remains in some cases anyway event in EF 5.0Circinus
D
0

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.

Desiccator answered 26/7, 2013 at 17:20 Comment(0)
K
0

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

Koziel answered 17/12, 2014 at 14:10 Comment(0)
S
0

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.

Swinish answered 5/8, 2015 at 16:33 Comment(0)
R
0

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

Reinhart answered 26/5, 2020 at 15:34 Comment(0)
M
0

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

Macdonald answered 31/8, 2020 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.