Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Seed() method of Configuration class of migrations

I have 2 questions:

1) How can I run Seed() method from the package-manager console without updating-database model?

2) Is there a way how to call Seed() method in the code?

Thx for any advice.

like image 700
Maris Avatar asked May 28 '13 08:05

Maris


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.

How do I seed data in .NET core?

Here is the sample code. After adding this piece of code, we need to build the application. Now, let's open Package Manager Console . We need to run "add-migration user_role" and "update-database" commands in order to reflect seeding changes in database.

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

Seed Data in Entity Framework Core So as soon as we execute our migration files to create and configure the database, we want to populate it with some initial data. This action is called Data Seeding. So, we are using the HasData method to inform EF Core about the data it has to seed.


1 Answers

Answering your first question. Create a Migration by running add-migration SeedOnly

Clear out all Up() and Down() code generated if there was any pending changes

public partial class SeedOnly : DbMigration {     public override void Up()     {     }      public override void Down()     {     } } 

Then you can Target a Specific Migration by running update-database -TargetMigration SeedOnly in the Package Manager console

like image 200
De Wet Ellis Avatar answered Sep 24 '22 10:09

De Wet Ellis