Adding a New Column to an Existing Table in Entity Framework

≯℡__Kan透↙ 提交于 2019-12-03 11:36:10

问题


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:


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



回答2:


An addendum to the answer by alltej above, and answering Chris Walsh's reply that he is getting 'The conceptual side Member or Property 'xxxxx' specified as part of this MSL does not exist in MetadataWorkspace." on the new Scalar property line under '

You have to make sure that you search 'Add the property ' in TWO places within your .edmx file , otherwise you will get Chris's error




回答3:


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; }




回答4:


Figured out the problem. When I generated the model I was getting an Error 113: Multiplicity is not valid in Role. I didn't notice it among the other 16307 errors that were generated when the creation failed. Once I fixed that everything worked fine.

Thanks




回答5:


WARNING: If you can't delete your database, DO NOT follow these steps!

I am using Visual Code (.net 2). Here is how I solved it:

First, delete your database:

dotnet ef database drop

Then, remove all the migrations:

dotnet ef migrations remove

Run the same command untill all migrations are deleted!

Add your first migration:

dotnet ef migrations add InitialCreate

After running the command listed above, make sure to make the changes at the new file generated, probably at Data/Migrations file: timestamp+InitialCreate not the desing file.

Image example: Adding new Column

After adding your collumns and everything is consistent, save the project and run the following command:

dotnet ef database update

If everything is ok, your new column will be created.




回答6:


I just found this question when I had a situation where I added some columns to a table, and my edmx file would update just fine; however, the code generation was not triggering properly, so my .cs files were not updating. I right clicked on the Entities.tt file and selected "Run Custom Tool" which runs the text transformations, which fixed it for me.



来源:https://stackoverflow.com/questions/35950795/adding-a-new-column-to-an-existing-table-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!