Kri*_*all 6 migration entity-framework-4.1 ef-migrations
使用实体框架迁移(Beta1),在开发过程中使用Update-Database命令都很好.
但是当应用程序在某个客户的服务器上运行时,我真的希望我的应用程序在启动时自动将其数据库模式更新为最新版本.
这可能吗?文档很少.
Bet*_*tty 11
在RTM之前,他们没有提供这样做的方法,此时他们已经承诺了命令行应用程序和msdeploy提供程序.资料来源:http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx
当然不满意,powershell命令存储在packages目录中并且是纯文本,它似乎只是加载一个名为EntityFramework.Migrations.Commands的程序集,存储在同一目录中.
通过该组件追踪我想出了以下内容
public class MyContext : DbContext
{
static MyContext()
{
DbMigrationsConfiguration configuration = new DbMigrationsConfiguration() {
MigrationsAssembly = typeof(MyContext).Assembly,
ContextType = typeof(MyContext),
AutomaticMigrationsEnabled = true,
};
DbMigrator dbMigrator = new DbMigrator(configuration);
dbMigrator.Update(null);
}
}
Run Code Online (Sandbox Code Playgroud)
更新:经过一些实验,我想出了更多的东西
这意味着代码被简化为
DbMigrator dbMigrator = new DbMigrator(new NAMESPACE.TO.MIGRATIONS.Configuration());
dbMigrator.Update(null);
Run Code Online (Sandbox Code Playgroud)
小智 7
此问题的另一个选项是添加
Database.SetInitializer<MyContext>(new MigrateDatabaseToLatestVersion<MyContext, NAMESPACE.TO.MIGRATIONS.Configuration>());
Run Code Online (Sandbox Code Playgroud)
符合你的Global.asax Application_Start方法。
| 归档时间: |
|
| 查看次数: |
6637 次 |
| 最近记录: |