How to update changes made to SampleData Seed in C # without resetting the database?

I am new to Entity Framework and MVC 4, and I am following the tutorial in the Music Store app on the Microsoft website (with some easy adaptations to my needs). I have a remote database created using First code. This includes the SampleData class, which includes information about songs, artists, genres, etc ... and it populates the database with this after creating all the tables. This works fine.

However, I have now made some changes to my SampleData class, and I want it to SampleData the database with this data (delete deleted rows, add new ones that were added, etc.). without dropping the database. My database administrator does not allow me to run the DROP command, so using DropCreateDatabaseAlways with my seed will not work. I did this locally and it works by dropping the database, creating it again with tables (the model is the same) and new data samples.

Any way to do this without dropping the database every time I want to update sample data? Maybe First First Migrations (although my model has not changed, just data)?

+4
source share
1 answer

This method depreciates, just skip the bottom.

You can use the first code data migrations.

To update the data samples (called seed in EF), you can edit Configuration.cs in your Migrations folder and add or update the data you want to change there, like this:

 protected override void Seed(icicle.Models.icicleEntities context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. Eg context.Widgets.AddOrUpdate(new Widget() { Id = 1, Name = "My Really Awesome Widget" }); } 

In the new paradigm, you just need to go to "Tools" → "Library Package Manager" → "Package Manager" and run the following line of script:

 Update-Database -ConnectionStringName "YourConnectionStringNameGoesHere" -Verbose 
+6
source

All Articles