222
schangxiang@126.com
2025-05-02 8472b30bf3ee867a226cc4345c13599d4d027a4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using CMS.Plugin.HIAWms.Domain.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Volo.Abp.DependencyInjection;
 
namespace CMS.Plugin.HIAWms.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();
    }
}