Thursday, May 14, 2009

ADO.NET Entity Data Model Designer missing delete functionality

I understand that the ADO.NET Entity Data Model Designer is still in its infancy, but to not delete an object all the way is a BIG missing feature in my opinion.

If you delete an object as I describe below, you may get the following error:

Error    1    Error 3013: Problem in Mapping Fragment starting at line 241: Missing table mapping: Foreign key constraint 'FK_TestTable1_MyTable' from table TestTable1 (MyTableID) to table MyTable(ID): no mapping specified for the table TestTable1.
    C:\MyWebSite\DataModel\Model.edmx    242    15    DataModel

For simplicity in explanation, let’s assume you have two tables in your model. They are called TestTable1 and MyTable. There is a foreign key / association inTestTable1 that points to MyTable. There is also a reverse association from MyTable to TestTable1.

I decided that I didn’t want TestTable1 in the model anymore. I select the object and delete it. That only deletes it from the viewable portion of the model. It is still in the underlying xml.

You can try to update the model from the database, but it doesn’t show TestTable1 as a table that you can add again. This is because it is still defined in the underlying xml.

Luckily the workaround is pretty easy and painless, but not necessarily obvious at first glance. Do the following to remove the references to TestTable1 and fix the problem.

  1. Right-click on the Model.edmx (or similarly named file) in the Solution Explorer in Visual Studio 2008.
  2. Choose Open With…
  3. In the windows that comes up, choose XML Editor and click the OK button.
  4. If you are prompted to close the model because it is already open, click the Yes button.
  5. Now, search for your table name. In my case TestTable1. Delete all tags (and inner-Xml) that you find.
  6. Save your changes.
  7. Right-click on the Model.edmx (or similarly named file) in the Solution Explorer in Visual Studio 2008.
  8. Choose Open With…
  9. In the windows that comes up, choose ADO.NET Entity Data Model Designer and click the OK button.
  10. Rebuild your solution. That should take care of the error.

Another workaround is to actually delete the table from your database. This may or may not be what you want to do. If you do indeed want to delete it from the database also, then you are in luck. You can update the underlying xml by updating the model from the database (right-click on the designer surface).

If you want to see a read-only visual representation of the underlying xml look for a Model Browser tab while you are in the ADO.NET Entity Data Model Designer. Mine is right next to my Solution Explorer on the right side of my screen. If it isn’t there check the different dockable areas. The Model Browser only shows when you are in the ADO.NET Entity Data Model Designer, so be sure that is your active window. It would be nice if you could edit through the Model Browser. Oh well, your choices for now are direct xml editing or modifying your database and synching it.

2 comments:

Anonymous said...

Thank you. That fixed a problem I had.

endoch said...

after 5 years, this post is still helping me on fixing my problem.

many thanks!