如何在 URL for an ASP.Net Core Web API 中区分具有相同结构和相同上下文的多个数据库
How to distinguish between multiple databases with the same structure and same context in the URL for an ASP.Net Core Web API
我正在尝试在 asp.net 核心中创建一个 Web api,它可以针对具有完全相同结构的同一服务器上的多个数据库。因此,他们应该能够共享相同的上下文和控制器。为每个数据库创建单独的上下文和控制器会很快失控,并且很难维护。因此,我应该能够在 Startup.cs.
中执行此操作
services.AddDbContext<DatabaseContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("db1")));
services.AddDbContext<DatabaseContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("db2")));
services.AddDbContext<DatabaseContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("db3")));
我 运行 遇到的问题是如何区分查询的是哪个数据库。理想情况下,我希望 url 可以是 ...{database}[controller]... 其中数据库参数可用于确定正在查询哪个数据库。我已经尝试研究多租户方法,但是我无法弄清楚如何使其适应我的特定用例。
如有任何帮助,我们将不胜感激。
看看这个:
您可以通过继承定义 DbSet 的主要数据库上下文来使用多个数据库上下文来完成此操作。
创建基本上下文并将所有设置包含在其中,DBSET:
public abstract class BaseContext : DbContext
{
public BaseContext(DbContext options)
: base(options)
{ }
public DbSet<object> FirstDbSet { get; set; }
...
}
从两个 DB(数据库)的 BaseContext 继承:
public class NavaContext : BaseContext
{
public NavaContext (DbContext<NavaContext> options) : base(options)
{
}
}
public class StackContext : BaseContext
{
public StackContext(DbContext<StackContext> options) : base(options)
{
}
}
并在 Startup.cs 中注册:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddDbContext<NavaContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LATAMConnectionString")));
services.AddDbContext<StackContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EUConnectionString")));
// Autofac
var builder = new ContainerBuilder();
// needed only if you plan to inject ICollection<BaseContext>
builder.RegisterType<NavaContext>().As<BaseContext>();
builder.RegisterType<StackContext>().As<BaseContext>();
builder.Populate(services);
return new AutofacServiceProvider(builder.Build());
}
在appsettings.json中添加连接字符串:
"ConnectionStrings": {
"NavaConnectionString": "Server=(localdb)\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true",
"StackConnectionString": "Server=(localdb)\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
}
现在您可以注入两个上下文:
public class ReportRepository : IReportRepository
{
private readonly NavaContext latamDbContext;
private readonly StackContext euDbContext;
public ReportRepository(NavaContext latamDbContext, StackContext euDbContext)
{
this.latamDbContext = latamDbContext;
this.euDbContext = euDbContext;
}
}
或者如果您打算注入上下文集合:
public class ReportRepository : IReportRepository
{
private readonly ICollection<BaseContext> dbContexts;
public ReportRepository(ICollection<BaseContext> dbContexts)
{
this.dbContexts = dbContexts;
}
}
访问特定上下文:
var _stackContext= dbContexts.FirstOrDefault(x => x is StackContext) as StackContext;
var _navaContext= dbContexts.FirstOrDefault(x => x is NavaContext) as NavaContext;
在 dot net core 中有多个数据库时创建连接的最佳方法是 IDesignTimeDbContextFactory
public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
public BloggingContext CreateDbContext(string[] args)
{
// logic to determine which connection string to use
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlite("connectionString");
return new BloggingContext(optionsBuilder.Options);
}
}
使用注册服务注入 BloggingContextFactory 并传递 args[] 逻辑以使用连接字符串
我正在尝试在 asp.net 核心中创建一个 Web api,它可以针对具有完全相同结构的同一服务器上的多个数据库。因此,他们应该能够共享相同的上下文和控制器。为每个数据库创建单独的上下文和控制器会很快失控,并且很难维护。因此,我应该能够在 Startup.cs.
中执行此操作services.AddDbContext<DatabaseContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("db1")));
services.AddDbContext<DatabaseContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("db2")));
services.AddDbContext<DatabaseContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("db3")));
我 运行 遇到的问题是如何区分查询的是哪个数据库。理想情况下,我希望 url 可以是 ...{database}[controller]... 其中数据库参数可用于确定正在查询哪个数据库。我已经尝试研究多租户方法,但是我无法弄清楚如何使其适应我的特定用例。
如有任何帮助,我们将不胜感激。
看看这个:
您可以通过继承定义 DbSet 的主要数据库上下文来使用多个数据库上下文来完成此操作。
创建基本上下文并将所有设置包含在其中,DBSET:
public abstract class BaseContext : DbContext
{
public BaseContext(DbContext options)
: base(options)
{ }
public DbSet<object> FirstDbSet { get; set; }
...
}
从两个 DB(数据库)的 BaseContext 继承:
public class NavaContext : BaseContext
{
public NavaContext (DbContext<NavaContext> options) : base(options)
{
}
}
public class StackContext : BaseContext
{
public StackContext(DbContext<StackContext> options) : base(options)
{
}
}
并在 Startup.cs 中注册:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddDbContext<NavaContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LATAMConnectionString")));
services.AddDbContext<StackContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EUConnectionString")));
// Autofac
var builder = new ContainerBuilder();
// needed only if you plan to inject ICollection<BaseContext>
builder.RegisterType<NavaContext>().As<BaseContext>();
builder.RegisterType<StackContext>().As<BaseContext>();
builder.Populate(services);
return new AutofacServiceProvider(builder.Build());
}
在appsettings.json中添加连接字符串:
"ConnectionStrings": {
"NavaConnectionString": "Server=(localdb)\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true",
"StackConnectionString": "Server=(localdb)\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
}
现在您可以注入两个上下文:
public class ReportRepository : IReportRepository
{
private readonly NavaContext latamDbContext;
private readonly StackContext euDbContext;
public ReportRepository(NavaContext latamDbContext, StackContext euDbContext)
{
this.latamDbContext = latamDbContext;
this.euDbContext = euDbContext;
}
}
或者如果您打算注入上下文集合:
public class ReportRepository : IReportRepository
{
private readonly ICollection<BaseContext> dbContexts;
public ReportRepository(ICollection<BaseContext> dbContexts)
{
this.dbContexts = dbContexts;
}
}
访问特定上下文:
var _stackContext= dbContexts.FirstOrDefault(x => x is StackContext) as StackContext;
var _navaContext= dbContexts.FirstOrDefault(x => x is NavaContext) as NavaContext;
在 dot net core 中有多个数据库时创建连接的最佳方法是 IDesignTimeDbContextFactory
public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
public BloggingContext CreateDbContext(string[] args)
{
// logic to determine which connection string to use
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlite("connectionString");
return new BloggingContext(optionsBuilder.Options);
}
}
使用注册服务注入 BloggingContextFactory 并传递 args[] 逻辑以使用连接字符串