Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a New Column to an Existing Table in Entity Framework

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.

like image 684
Rodney Buxton Avatar asked Mar 11 '16 22:03

Rodney Buxton


People also ask

How do I add a column to an already created table?

The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows. ALTER TABLE table_name ADD column_name datatype; The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as follows.

Can we insert a new column into a table?

Add a column to the left or right Click in a cell to the left or right of where you want to add a column. Under Table Tools, on the Layout tab, do one of the following: To add a column to the left of the cell, click Insert Left in the Rows and Columns group.


2 Answers

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

like image 160
alltej Avatar answered Sep 21 '22 05:09

alltej


  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 again <EntityType Name="YourTableNameToAddColumn">, there is a second one.

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

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

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

  8. Save the *.edmx file

  9. After that update the edmx model of your table public string YourNewColumnName { get; set; }

like image 28
Nestoras Rose Avatar answered Sep 21 '22 05:09

Nestoras Rose