Adding a New Column to an Existing Table in Entity Framework

Rodney Buxton picture Rodney Buxton · Mar 11, 2016 · Viewed 49.7k times · Source

I have added a new column to a table in my database. The table is already defined in the existing Entity Framework model. I've been through most of the items here on how to do this and it still fails.

A little background, this entity model has not been updated in at least 3 years. So aside from the column I'm adding I know there have been a number of other columns that have been added in that time, but never included. I took over the project about 9 months ago and have never been able to successfully update the model.

First attempt:

  • Opened the Model in Visual Studio
  • Right Clicked on the Background
  • Clicked on "Update Model From Database..."
  • Clicked on the Refresh Tab
  • Selected Tables
  • Highlighted the specific table
  • Clicked Finish

Result:

  • Classes for Dozens of tables that were in my model were deleted
  • The table in question was not updated

Second Attempt

  • Restored all Source
  • Same as above but
  • After opening the Update Wizard, Clicked the Delete Tab
  • Selected Tables
  • Clicked Finish
  • All the Tables were deleted
  • Saved the EF Model/Closed/Opened it
  • Went back to the Update Wizard Add Tab
  • Clicked Tables
  • None of my tables were displayed when I expanded everything
  • Checked the checkbox at the Tables level

Result

  • None of my tables were added back, but anything that was not originally included was added

Third Attempt

  • Restored all source
  • Deleted the two .tt files
  • Opened the Update Wizard
  • Clicked Add for Everything

Result

  • Nothing was recreated, no .tt files or anything else.

Fourth Attempt

  • Restored Source
  • Deleted Table from the EF Model
  • Opened Update Wizard
  • Clicked Add Tables

Results

  • Classes for Dozens of tables that were in my model were deleted
  • The table in question was not added back

Final Attempt

  • Added entity manually to model

Result

  • Code all compiled and ran, but values were never retrieved from the DB or updated

Any help or direction that could be provided would be greatly appreciated as I'm at a critical point and have to get the model updated.

Answer

alltej picture alltej · Aug 5, 2016

The "Update Model from Database" is hard/slow to use and is prone to errors. It generates other stuff that you probably don't want/need. So manually adding the column that you need will work better. I suggest you do it outside the VS editor since depending on how many models/tables, it can be very slow opening the file in VS.

  1. So in Windows Exlorer, right-click on the *.edmx file and open with Notepad (or Notepad++/Textpad).

  2. Search for the text <EntityType Name="YourTableNameToAddColumn">.

  3. Add the property <Property Name="YourNewColumnName" Type="varchar" MaxLength="64" />

  4. Search for the text <MappingFragment StoreEntitySet="YourTableNameToAddColumn">

  5. Add mapping to the new column <ScalarProperty Name="YourNewColumnName" ColumnName="YourNewColumnName"/>

  6. Save the *.edmx file