Adding a new column to an existing table in the Entity framework

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

A small background, this entity model has not been updated for at least 3 years. Therefore, in addition to the column that I am adding, I know that several other columns were added at that time, but they were never included. I took over the project about 9 months ago and was never able to successfully update the model.

First try:

  • Opened a model in Visual Studio
  • Right click on the background
  • Click "Update Model from Database ..."
  • Click on the Refresh tab
  • Selected Tables
  • Highlighted a specific table
  • Click Finish

Result:

  • The classes for the dozens of tables that were in my model have been deleted.
  • The corresponding table was not updated.

Second attempt

  • Recovered source
  • Same as above but
  • After opening the update wizard, click on the "Delete" tab
  • Selected Tables
  • Click Finish
  • All tables have been deleted.
  • Saved model EF / closed / open.
  • Returned to the Add Updater Wizard tab
  • Click tables
  • None of my tables displayed when I expanded everything
  • Checkbox checked

Result

  • None of my tables were added back, but everything that was not originally added.

Third attempt

  • Restored entire source
  • Two .tt files removed
  • Opened update wizard
  • Click Add For Everything.

Result

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

Fourth attempt

  • Recovered source
  • Remote table from EF model.
  • Open Updater Wizard
  • Click "Add Tables"

results

  • The classes for the dozens of tables that were in my model have been deleted.
  • The following table has not been added.

Final attempt

  • Manually added object for modeling

Result

  • The code compiles and runs, but the values โ€‹โ€‹are never retrieved from the database or updated

Any help or direction that can be provided will be greatly appreciated, as I am in critical condition and must update the model.

+14
source share
6 answers

"Update model from database" is difficult / slow to use and error prone. It generates other things that you probably don't need / need. So manually adding the column you need will work better. I suggest you do this outside of the VS editor, because depending on how many models / tables it can open a file very slowly 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 
+19
source

In addition to alltej's answer above and responding to Chris Walsh, he replies that he gets the "Conceptual member or property" xxxxx "specified as part of this MSL does not exist in MetadataWorkspace." in a new line of Scalar properties under <

You must make sure that you are looking for the โ€œAdd Propertyโ€ in TWO strong> places in your .edmx file, otherwise you will get a Chris error

+7
source

I found out the problem. When I generated the model, I got error 113: the multiplicity is not valid in the role. I did not notice this among other errors of 16307 which were generated at unsuccessful creation. As soon as I fixed that everything was working fine.

thanks

+2
source

1. In Windows Exlorer, right-click the * .edmx file and open it using Notepad (or Notepad ++ / Textpad).

2. Find the text <EntityType Name="YourTableNameToAddColumn"> .

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

<EntityType Name="YourTableNameToAddColumn"> text again <EntityType Name="YourTableNameToAddColumn"> , there is a second.

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

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

<ScalarProperty Name="YourNewColumnName" ColumnName="YourNewColumnName"/> mapping to a new column <ScalarProperty Name="YourNewColumnName" ColumnName="YourNewColumnName"/> .

8. Save the * .edmx file

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

+1
source

WARNING: If you cannot delete your database, DO NOT follow these steps!

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

First delete your database:

 dotnet ef database drop 

Then remove all migrations:

 dotnet ef migrations remove 

Run the same command until all !

Add the first migration:

 dotnet ef migrations add InitialCreate 

After executing the command indicated above, be sure to make changes to the new file generated, possibly to the Data / Migrations file: timestamp + InitialCreate, not the desing file .

Sample Image: Adding a New Column

After adding your matches, and everything will be agreed, save the project and run the following command:

 dotnet ef database update 

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

0
source

I just found this question when a situation arose when I added some columns to the table and my edmx file would update very well; however, the code generation did not start properly, so my .cs files were not updated. I right-clicked on the Entities.tt file and selected โ€œRun Custom Toolโ€, which launches the text transformations, which fixed it for me.

0
source

Source: https://habr.com/ru/post/953155/


All Articles