Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework migration and seeding specific update

Tags:

does anyone know how you can seed a specific update using EF 5 migrations? I have existing database, alredy has lookups populated, and am developing some Audit functionality. I have created an AuditType entity and Audit. When I call update-database, I don't want all my seed data from when I first created the database to be re-added. Do I simply have to manuall delete the existing seed data out or can I do something like name a Configuration.cs with the datetime similar to what gets created when I call add-migration?

Thanks

like image 557
CheGuevarasBeret Avatar asked Aug 27 '13 18:08

CheGuevarasBeret


People also ask

What is the purpose of the seed method in migrations?

Seed method is used to initialized the database tables with some starting data. Whenever you run the migration and update the database it will run the seed method. Mostly it is used during the testing phase where you often need to recreate the database and populate database tables with sample data.

Which method can be used to seed initial data in database using Entity Framework Core?

We can use the HasData method of the ModelBuilder to seed the initial data into the database via migrations.


2 Answers

You can run a specific migration by specifying the name of the migration. For example, If you have a migration called MyTuesdayMigration.cs, In the package manager console, you would run this command:

update-database -TargetMigration MyTuesdayMigration 
like image 59
Greg Avatar answered Oct 01 '22 12:10

Greg


You may need to delete data so you should use -fore update-database -TargetMigration MigrationName -force

like image 39
foluis Avatar answered Oct 01 '22 11:10

foluis