如何运行种子()配置类迁移的方法 [英] How to run Seed() method of Configuration class of migrations

查看:155
本文介绍了如何运行种子()配置类迁移的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题:

1)如何运行种子从包管理器控制台()方法,而不更新数据库模型

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

2)有没有办法如何调用代码中的种子()方法吗?任何建议。

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

THX。

推荐答案

研究后,我终于找到了这个问题的解决方法:

After research I finally found the workaround for this issue:

1)让配置公开:

public sealed class Configuration : DbMigrationsConfiguration<YourContextClassHere>



2)添加下方的任意位置的代码。它将运行最新的迁移和更新数据库:

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();

这篇关于如何运行种子()配置类迁移的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆