How to run Seed() method of Configuration class of

2019-01-18 08:25发布

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.

8条回答
我命由我不由天
2楼-- · 2019-01-18 09:04

If you use context initiliazer as MigrateDatabaseToLatestVersion, seed method in configuration should be run automatically. Don't think you need to call it manually.

查看更多
Lonely孤独者°
3楼-- · 2019-01-18 09:13

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();
Debug.Write(script);

//This will run the migration update script and will run Seed() method
migrator.Update();
查看更多
登录 后发表回答