4

我需要确保我的应用程序本身能够升级他的数据库模型(应用迁移)

在 ABP 架构中,我应该在哪里调用 Migrate?

context.Database.Migrate();

由于这是对基础架构逻辑(实体框架核心)的调用,因此应远离域服务和应用程序服务。

提前致谢

4

2 回答 2

5

我终于找到了一个可行的解决方案,适用于 abp 4.3 此代码允许您的应用程序在启动时应用迁移。

public override void PostInitialize()
{
    var dbContextProvider = IocManager.Resolve<IDbContextProvider<ExtranetDbContext>>();
    var unitOfWorkManager = IocManager.Resolve<IUnitOfWorkManager>();

    using (var unitOfWork = unitOfWorkManager.Begin())
    {
        var context = dbContextProvider.GetDbContext(MultiTenancySides.Host);
        
        //Removes actual connection as it has been enlisted in a non needed transaction for migration
        context.Database.CloseConnection();
        context.Database.Migrate();
    }

    if (!SkipDbSeed)
    {
        SeedHelper.SeedHostDb(IocManager);
    }
}

IUnitOfWorkManager可以在此处找到有关的详细信息。

于 2019-03-05T20:16:25.977 回答
0

您好,您可以在 EntityFrameworkCoreModule 的 PostInitialize 方法中执行数据库迁移。

 public class MyApplicationEntityFrameworkCoreModule : AbpModule
    {

        public override void PostInitialize()
        {
            if (!SkipDbSeed)
            {
                SeedHelper.SeedHostDb(IocManager);
            }

            // --> You can execute migrations here <--
        }
    }
于 2017-07-17T06:23:39.240 回答