asp.net-core - 如何在 ABP Core Zero 中使用多个数据库?

标签 asp.net-core .net-core aspnetboilerplate

我想将“帐户”表与“数据”表隔离开,以便在另一个应用程序上重用。
我尝试使用 .NET Core 2.0 + Angular 模板,创建 2 个连接字符串,但是当创建另一个 AbpDbContext 时,我无法为上下文设置连接字符串。
在其 GitHub 上使用多个 DB 上下文的示例使用 .NET 框架,而不是 .NET 核心,允许在 dbcontext ctor 上设置连接字符串。
如何在 .net core 2 模板上执行此操作?

最佳答案

在 ASP.NET ZERO/ASP.NET BOILERPLATE 中连接多个数据库。

备注 - 使用单独的数据库上下文来使用多个数据库。

第 1 步。在“MultipleDbContextEfCoreDemo.Core”项目中为您的表创建模式类。

[Table ("tblStudent")] //Is in First Database
public class Student : Entity<long> {
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    protected Student () { }
}

[Table ("tblCourses")] //Is in Second Database
public class Courses : Entity<long> {
    public int ID { get; set; }
    public string CourseName { get; set; }
    public string Standard { get; set; }

    protected Courses () { }
}

第 2 步。在同一个项目(“MultipleDbContextEfCoreDemo.Core”项目)中创建/使用“MultipleDbContextEfCoreDemoConsts.cs”文件来添加数据库连接名称。
public class MultipleDbContextEfCoreDemoConsts
    {
        public const string LocalizationSourceName = "MultipleDbContextEfCoreDemo";

        public const string ConnectionStringName = "Default";

        public const string SecondDbConnectionStringName = "Second";
    }

第三步。在“MultipleDbContextEfCoreDemo.EntityFrameworkCore”项目中,转到“EntityFrameworkCore”文件夹并为要连接的每个数据库连接创建单独的“DBContext”和“DbContextConfigurer”文件。

第一数据库设置 -

连接到第一个数据库所需的文件 -

1. FirstDbContext.cs
public class FirstDbContext : AbpDbContext, IAbpPersistedGrantDbContext {
    /* Define an IDbSet for each entity of the application */
    public DbSet<PersistedGrantEntity> PersistedGrants { get; set; }
    public virtual DbSet<Student> Student { get; set; }

    public FirstDbContext (DbContextOptions<FirstDbContext> options) : base (options) {

    }

    protected override void OnModelCreating (ModelBuilder modelBuilder) { }
}

2. FirstDbContextConfigurer
public static class FirstDbContextConfigurer {
    public static void Configure (DbContextOptionsBuilder<FirstDbContext> builder, string connectionString) {
        builder.UseSqlServer (connectionString);
    }

    public static void Configure (DbContextOptionsBuilder<FirstDbContext> builder, DbConnection connection) {
        builder.UseSqlServer (connection);
    }
}

第二数据库设置 -

连接到第二个数据库所需的文件 -

1. SecondDbContext.cs
public class SecondDbContext : AbpDbContext, IAbpPersistedGrantDbContext {
    /* Define an IDbSet for each entity of the application */
    public DbSet<PersistedGrantEntity> PersistedGrants { get; set; }
    public virtual DbSet<Student> Student { get; set; }

    public SecondDbContext (DbContextOptions<SecondDbContext> options) : base (options) {

    }

    protected override void OnModelCreating (ModelBuilder modelBuilder) { }
}

2. SecondDbContextConfigurer
public static class SecondDbContextConfigurer {
    public static void Configure (DbContextOptionsBuilder<SecondDbContext> builder, string connectionString) {
        builder.UseSqlServer (connectionString);
    }
    public static void Configure (DbContextOptionsBuilder<SecondDbContext> builder, DbConnection connection) {
        builder.UseSqlServer (connection);
    }
}

第 4 步。然后在同一个项目(“MultipleDbContextEfCoreDemo.EntityFrameworkCore”)中添加“MyConnectionStringResolver.cs”
public class MyConnectionStringResolver : DefaultConnectionStringResolver
        {
            public MyConnectionStringResolver(IAbpStartupConfiguration configuration) 
                : base(configuration)
            {
            }

            public override string GetNameOrConnectionString(ConnectionStringResolveArgs args)
            {
                if (args["DbContextConcreteType"] as Type == typeof(SecondDbContext))
                {
                    var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());
                    return configuration.GetConnectionString(MultipleDbContextEfCoreDemoConsts.SecondDbConnectionStringName);
                }

                return base.GetNameOrConnectionString(args);
            }
        }

第 5 步。然后在同一个项目(“MultipleDbContextEfCoreDemo.EntityFrameworkCore”)中,更新“MultipleDbContextEfCoreDemoEntityFrameworkCoreModule.cs”文件,用我们的自定义实现 MyConnectionStringResolver 替换“IConnectionStringResolver”。
[DependsOn(typeof(MultipleDbContextEfCoreDemoCoreModule), typeof(AbpEntityFrameworkCoreModule))]
    public class MultipleDbContextEfCoreDemoEntityFrameworkCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.ReplaceService<IConnectionStringResolver, MyConnectionStringResolver>();

            // Configure first DbContext
            Configuration.Modules.AbpEfCore().AddDbContext<FirstDbContext>(options =>
            {
                if (options.ExistingConnection != null)
                {
                    FirstDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                }
                else
                {
                    FirstDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                }
            });

            // Configure second DbContext
            Configuration.Modules.AbpEfCore().AddDbContext<SecondDbContext>(options =>
            {
                if (options.ExistingConnection != null)
                {
                    SecondDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                }
                else
                {
                    SecondDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                }
            });
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(MultipleDbContextEfCoreDemoEntityFrameworkCoreModule).GetAssembly());
        }
    }

第 6 步。使用 Dto、接口(interface)和服务类在“MultipleDbContextEfCoreDemo.Application”项目中创建服务。

ITestAppService.cs-
public interface ITestAppService : IApplicationService
    {
        List<string> GetStudentAndCourses();

     }

TestAppService.cs
public class TestAppService : MultipleDbContextEfCoreDemoAppServiceBase, ITestAppService
    {
        private readonly IRepository<Student> _studentRepository; //in the first db
        private readonly IRepository<Courses> _coursesRepository; //in the second db

        public TestAppService(
            IRepository<Student> studentRepository,
            IRepository<Courses> coursesRepository
        )
        {
            _studentRepository = studentRepository;
            _coursesRepository = coursesRepository;
        }

        //a sample method uses both databases concurrently
        public List<string> GetStudentAndCourses()
        {
            List<string> names = new List<string>();

            var studentNames = _studentRepository.GetAllList().Select(p => "Student: " + p.FirstName).ToList();
            names.AddRange(peopleNames);

            var courseNames = _coursesRepository.GetAllList().Select(p => "Course: " + p.CourseName).ToList();
            names.AddRange(courseNames);

            return names;
        }
    }

第 7 步。将数据库连接字符串添加到您的 MultipleDbContextEfCoreDemo.Web/MultipleDbContextEfCoreDemo.Web.Host 项目的
“appsettings.json”文件。
{
  "ConnectionStrings": {
    "Default":
      "Server=XXX.XXX.XX.XX;Database=firstDB;Integrated Security=False;TrustServerCertificate=True;User ID=XX;Password=XXX;",
    "Second":
      "Server=XXX.XXX.XX.XX;Database=secondDB;Integrated Security=False;TrustServerCertificate=True;User ID=XX;Password=XXX;"
  }
  }

第 8 步。在您的 Angular/MVC 项目中使用服务。

关于asp.net-core - 如何在 ABP Core Zero 中使用多个数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49243891/

相关文章:

c# - 解决方案因缺少 OperationFilterContext.ControllerActionDescriptor 而编译失败

c# - 当 Controller 方法具有异步后缀时,AspNetCore 3.0(从 2.2 升级)路由似乎中断

Dockerfile 说复制失败 : in . NET Core Dockerfile

powershell - 您能否在 macOS 上的 Powershell 7 中使用 PtrToStringAuto 来解密安全字符串?

c# - 查询源已与表达式关联

postgresql - 如何在 ASP.NET Boilerplate Core 2.0 模板中使用 Npgsql?

c# - 在 ASP.NET Core 中填充下拉列表

asp.net-core - 如何使用 Microsoft.VisualStudio.Web.CodeGeneration?

C# .NET Core 2.1 Span<T> 和 Memory<T> 性能注意事项

asp.net-core - 为什么我应该选择 ASP.NET Core 和 .Net Core?