实体框架 4.3 在应用程序启动时运行迁移

发布于 2025-01-05 17:50:58 字数 46 浏览 1 评论 0原文

使用 EF 4.3 在应用程序启动时执行所有必需的数据库迁移的最佳方法是什么?

What is the best way to execute all required db migrations at application start with EF 4.3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落墨 2025-01-12 17:50:58

最好的方法应该是使用新的 MigrateDatabaseToLatestVersion 初始值设定项。

Database.SetInitializer<YourContext>(
    new MigrateDatabaseToLatestVersion<YourContext, YourMigrationsConfig>());
Database.Initialize(false);

The best way should be using new MigrateDatabaseToLatestVersion initializer.

Database.SetInitializer<YourContext>(
    new MigrateDatabaseToLatestVersion<YourContext, YourMigrationsConfig>());
Database.Initialize(false);
漫雪独思 2025-01-12 17:50:58

有关 EF 4.3 配置选项的详细说明,请访问 EF 4.3 配置文件设置。最后一节介绍了数据库初始化程序,包括新的 Code First MigrateDatabaseToLatestVersion 初始化程序。

尽管实体框架(与 .NET 4.x 的许多其他功能一样)更倾向于约定优于配置,但在这种情况下,通过应用程序的配置文件设置 MigrateDatabaseToLatestVersion 数据库初始值设定项可能非常有用,而不是通过应用程序的配置文件来设置而不是将其显式编码到您的应用程序中。

A great description of the EF 4.3 configuration options can be found at EF 4.3 Configuration File Settings on the ADO.NET team blog. The very last section describes Database Initializers, including the new Code First MigrateDatabaseToLatestVersion initializer.

Although Entity Framework—like so many other features of .NET 4.x—favors convention over configuration, this is one case where it might be very useful to set the MigrateDatabaseToLatestVersion database initializer through your application's config file rather than explicitly code it into your application.

忆悲凉 2025-01-12 17:50:58

我需要明确地执行此操作,因为我使用 uber-context 进行迁移,这是其他迁移的超集。关键点是:

var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(
    new Lcmp.EF.Migrations.Migrations.Configuration());
dbMigrator.Update();

通过少量的 Elmah 日志记录,我实际上使用了它,从 Application_Start() 调用。它的一部分是从别人的想法中偷来的。我不确定线程​​安全互锁部分是必要的。

public static int IsMigrating = 0;
private static void UpdateDatabase()
{
    try
    {
        if (0 == System.Threading.Interlocked.Exchange(ref IsMigrating, 1))
        {
            try
            {
                // Automatically migrate database to catch up.
                Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Checking db for pending migrations.")));
                var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(new Lcmp.EF.Migrations.Migrations.Configuration());
                var pendingMigrations = string.Join(", ", dbMigrator.GetPendingMigrations().ToArray());
                Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("The database needs these code updates: " + pendingMigrations)));
                dbMigrator.Update();
                Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Done upgrading database.")));
            }
            finally
            {
                System.Threading.Interlocked.Exchange(ref IsMigrating, 0);
            }
        }
    }
    catch (System.Data.Entity.Migrations.Infrastructure.AutomaticDataLossException ex)
    {  
        Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
    }
    catch (Exception ex)
    {
        Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
    }
}

I needed to do it explicitly, because I use an uber-context for migration, a superset of the other migrations. The key bit is:

var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(
    new Lcmp.EF.Migrations.Migrations.Configuration());
dbMigrator.Update();

With a sprinkling of Elmah logging, I actually use this, called from Application_Start(). Pieces of it are stolen from others' ideas. I'm not positive that the thread-safety Interlocked part is necessary.

public static int IsMigrating = 0;
private static void UpdateDatabase()
{
    try
    {
        if (0 == System.Threading.Interlocked.Exchange(ref IsMigrating, 1))
        {
            try
            {
                // Automatically migrate database to catch up.
                Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Checking db for pending migrations.")));
                var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(new Lcmp.EF.Migrations.Migrations.Configuration());
                var pendingMigrations = string.Join(", ", dbMigrator.GetPendingMigrations().ToArray());
                Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("The database needs these code updates: " + pendingMigrations)));
                dbMigrator.Update();
                Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Done upgrading database.")));
            }
            finally
            {
                System.Threading.Interlocked.Exchange(ref IsMigrating, 0);
            }
        }
    }
    catch (System.Data.Entity.Migrations.Infrastructure.AutomaticDataLossException ex)
    {  
        Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
    }
    catch (Exception ex)
    {
        Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文