Abp上手遇到的问题

起因

今天是入职的第一天,因为部门的人都比较忙,让我先上手Abp.对于Abp知道的并不多,也仅仅是使用DDD(领域驱动设计).

Abp官网地址:https://www.abp.io/

1. 遇到问题1

直接从官网下载脚手架(文件压缩包),便在VS中直接运行.
abp提供了脚手架和cli两种方式.
便遇到这个问题.
abp遇到无法打开数据库,Cannot open database
看这个错误,刚开始还以为还是数据库的问题,在数据库的连接字符串看到是使用的localdb的方式,虽然原先也没有使用过.但知道这个是本地的数据库(sqlserver的精简版),找不到错误,只能老老实实看代码.找到整个解决方案下只有两个配置连接字符串的地方.
abp脚手架带有数据库连接字符串
发现*.DbMigrator这个项目,比较可疑,便进行跟踪调试.
public async Task StartAsync(CancellationToken cancellationToken)
{
    using (var application = AbpApplicationFactory.Create<MyAbpAppDbMigratorModule>(options =>
    {
        options.UseAutofac();  //使用autofac容器替换asp.net core自带的容器
        options.Services.AddLogging(c => c.AddSerilog());  //使用serilog记录日志
    }))
    {
        application.Initialize();

        await application
            .ServiceProvider
            .GetRequiredService<MyAbpAppDbMigrationService>()
            .MigrateAsync();  //重点在这里

        application.Shutdown();

        _hostApplicationLifetime.StopApplication();
    }
}
public async Task MigrateAsync()
{
    try
    {
        if (DbMigrationsProjectExists() && !MigrationsFolderExists())
        {
            AddInitialMigration();  //重点在这里  
            return;
        }
    }
    catch (Exception e)
    {
        Logger.LogWarning("Couldn't determinate if any migrations exist : " + e.Message);
    }

    Logger.LogInformation("Started database migrations...");

    await MigrateDatabaseSchemaAsync();
    await SeedDataAsync();

    Logger.LogInformation($"Successfully completed host database migrations.");

    var tenants = await _tenantRepository.GetListAsync(includeDetails: true);

    var migratedDatabaseSchemas = new HashSet<string>();
    foreach (var tenant in tenants)
    {
        using (_currentTenant.Change(tenant.Id))
        {
            if (tenant.ConnectionStrings.Any())
            {
                var tenantConnectionStrings = tenant.ConnectionStrings
                    .Select(x => x.Value)
                    .ToList();

                if (!migratedDatabaseSchemas.IsSupersetOf(tenantConnectionStrings))
                {
                    await MigrateDatabaseSchemaAsync(tenant);

                    migratedDatabaseSchemas.AddIfNotContains(tenantConnectionStrings);
                }
            }

            await SeedDataAsync(tenant);
        }

        Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations.");
    }

    Logger.LogInformation("Successfully completed all database migrations.");
    Logger.LogInformation("You can safely end this process...");
}
private void AddInitialMigration()
{
    Logger.LogInformation("Creating initial migration...");

    string argumentPrefix;
    string fileName;

    if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    {
        argumentPrefix = "-c";
        fileName = "/bin/bash";
    }
    else
    {
        argumentPrefix = "/C";
        fileName = "cmd.exe";
    }
    //看到这里 基本知道是哪里的问题,没有安装abp的cli,所以执行abp是有问题的
    var procStartInfo = new ProcessStartInfo(fileName,
        $"{argumentPrefix} \"abp create-migration-and-run-migrator \"{GetDbMigrationsProjectFolderPath()}\"\""
    );

    try
    {
        Process.Start(procStartInfo);
    }
    catch (Exception)
    {
        throw new Exception("Couldn't run ABP CLI...");
    }
}

abp没有安装CLIENT,直接运行项目,会出现打不开数据库

看到这个,还是先看官方文档,老老实实安装Abp的CLI:

dotnet tool install -g Volo.Abp.Cli

再次运行*.HttpApi.Host项目.

运行*.HttpApi.Host项目效果图

2. 在数据库管理工具连接LocalDb

#Abp 模版中数据库连接字符串,是LocalDb
Server=(LocalDb)\\MSSQLLocalDB;Database=MyAbpApp;Trusted_Connection=True

将Server中的字符串拷贝到Sql Server管理工具,是连接不上数据库的.因为自己笔记本没有安装SqlServer数据库和管理工具,这里使用Vs2019的SqlServer 对象资源管理器进行连接SqlServer LocalDb版本.

使用Abp模版中的数据库连接字符串连接数据库.连接不上,提示的错误信息

数据库连接字符串,是没问题的,在程序中是可以进行连接的.到底是哪里的问题.一直在想是哪里的问题.

看到反斜杠,感觉是不是这个的问题.便删除一个反斜杠.再次连接,竟然可以连接了.

在SqlServer管理工具连接LocalDb,只能用单个反斜杠.

看到这里,觉得应该是SqlServer(SMSS管理工具)和VS中的SqlServer对象管理器,默认是对连接字符串进行转义,并且没有两个反斜杠进行处理.造成无法连接LocalDb的实例.


秋风 2021-03-22