Tables don't show when re-adding them to entity-model (edmx)
Asked Answered
L

5

18

I have a db with 5 tables. At the beginning, I've added those tables in, but then decided to remove some due to some relationship compile error.

Now, when i want to add them back, i'm opening the edmx file -> update model from database... I don't see those tables under add tab, but only under the "refresh" tab.

How can i add them back?

Leesa answered 3/2, 2010 at 3:35 Comment(1)
Right click at the edmx level and say add to TFS.Ananias
M
29

In order to re-add a table to your model you will first need to delete the table from your model. (a list of tables is visible in the [model.Store] tree (see 'Model Browser' pane- you can open it from right-mouse click menu). When you run 'Update model from database...' the table will appear in the 'Add' tab in the first step of the 'Update Wizard'.

Steps to complete:

  1. Close your model in Visual Studio.
  2. Open your .edmx file in a text editor.
  3. Search and delete the xml entity elements (see notes below).
  4. Open your model in Visual Studio.
  5. Click Update Model from Database.

To delete all references to a table in your Model:

  • In the 'EntityContainer' element, delete all 'EntitySet' child elements that have the 'Name' attribute set to the value [TableNameToReAdd].
  • In the 'EntityContainer' element, delete all 'AssociationSet' child elements where an 'End' element exists that have their 'EntitySet' attribute set to the value [TableNameToReAdd].
  • In the 'EntityContainer' element, delete all 'EntityType' child elements where that have the 'Name' attribute set to the value [TableNameToReAdd].
  • In the 'EntityContainer' element, delete all 'Association' child elements where an 'End' element exists that have their 'Role' attribute set to the value [TableNameToReAdd].
Morel answered 5/2, 2010 at 16:48 Comment(1)
Very good answer; this helped me solve the problem. I don't know if things have changed since this answer was published, but the solution seems more simple now. Use the Model Browser window for the edmx file, and find the table you want to re-add in the Store tree. Simply delete the node, save the edmx, and the table will be available again in the "add tables" dialog box.Raggletaggle
R
10

I reading this and other searching option, but finally I have found another answered which help me to short-out this issue.

From the error message looks like one of your table/view doesn't have a primary key. EF needs every table to have a primary key in order to generate Entity keys. You may still be able to run your application, but I strongly suggest you add primary keys as warned.

Link which solved my issue.

Updated

If some time you done everything fine, still does not reflect the changes either adding a new column or change data-type.

The best way of this try to update manually, still there is no hope, then as suggested @mathijsuitmegen, delete and add the table, but this is last option to prefer.

Reagent answered 11/7, 2015 at 14:40 Comment(2)
This was my issue. Missing Primary key on the table I was trying to import.Gardas
this was my issue.Nourishing
S
3

An easier solution is in the Model Browser under

[modelName].Store -> Tables/ViewS

delete the table not showing up. Then right click on the model "update from database" and the table should be there.

Staceestacey answered 14/1, 2016 at 9:32 Comment(0)
M
2

You want to re-add an entity after it is deleted in the model.
Besides editing the edmx file there's another way to do this.
You will have to temporary delete the table from the database.

note: I would only to this if the database is not in production yet!

So in SQL Server Management Studio first create a script:
right click on the table(s) that correspond with the missing entity in your model. Select 'Script Table as', 'CREATE To', 'New Query Editor Window'.

The Second step is to delete the table. Right click again and select 'Delete'. Confirm the delete.

Back in Visual Studio do an update of the model.

Go back to SQL Server Management Studio and run the 'create' script you have just created.
The table will be added to your database again.

In Visual Studio you can now do an update again, your table will show up under the 'add ' tab!!!

Morel answered 13/10, 2010 at 7:51 Comment(0)
J
0

In addition to the above list of references that need to be deleted from the model please consider removing AssociationSetMapping element as well in case if your table has associative relationship with other tables.

Joanejoanie answered 7/12, 2013 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.