Adding a New Column to an Existing Table in Entity Framework

database-first ef-database-first entity-framework

Question

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.

1
20
9/22/2018 5:44:50 PM

Popular Answer

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

27
1/16/2020 11:32:32 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow