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

c# code-first ef-migrations entity-framework


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.

5/22/2018 8:20:06 AM

Accepted Answer

After research I finally found the workaround for this issue:

1) Make Configuration public:

public sealed class Configuration : DbMigrationsConfiguration<YourContextClassHere>

2) Add the code below anywhere. It will run the latest migration and update your database:

Configuration configuration = new Configuration();
configuration.ContextType = typeof(YourContextClassHere);
var migrator = new DbMigrator(configuration);

//This will get the SQL script which will update the DB and write it to debug
var scriptor = new MigratorScriptingDecorator(migrator);
string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null).ToString();

//This will run the migration update script and will run Seed() method
5/29/2016 6:30:21 AM

Popular Answer

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

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow