using CMS.Plugin.WareCmsUtilityApi.Domain.Data;
|
using Microsoft.EntityFrameworkCore;
|
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.Logging;
|
using Volo.Abp.DependencyInjection;
|
|
namespace CMS.Plugin.WareCmsUtilityApi.EntityFrameworkCore;
|
|
/// <inheritdoc />
|
public class CMSPluginDbSchemaMigrator : ICMSPluginDbSchemaMigrator, ITransientDependency
|
{
|
private readonly IServiceProvider _serviceProvider;
|
private readonly ILogger<CMSPluginDbSchemaMigrator> _logger;
|
|
/// <summary>
|
/// Initializes a new instance of the <see cref="CMSPluginDbSchemaMigrator"/> class.
|
/// </summary>
|
/// <param name="serviceProvider">The service provider.</param>
|
public CMSPluginDbSchemaMigrator(IServiceProvider serviceProvider, ILogger<CMSPluginDbSchemaMigrator> logger)
|
{
|
_serviceProvider = serviceProvider;
|
_logger = logger;
|
}
|
|
/// <inheritdoc />
|
public async Task MigrateAsync()
|
{
|
/* We intentionally resolving the CMSDbContext
|
* from IServiceProvider (instead of directly injecting it)
|
* to properly get the connection string of the current tenant in the
|
* current scope.
|
*/
|
var database = _serviceProvider.GetRequiredService<ICMSPluginDbContext>().Database;
|
_logger.LogInformation($"Start database {database.GetDbConnection().Database} migrate");
|
await database.MigrateAsync();
|
}
|
}
|