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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With