C# 具有多个数据库的存储库模式
我在windows服务中使用EF Core和Autofac上的存储库模式 我有一个服务需要连接十几个数据库,这些数据库具有相同的模式(相同的dbcontext),但只有不同的数据。 如何使用Autofac在我的服务中实现这一点?贝洛C# 具有多个数据库的存储库模式,c#,.net,.net-core,entity-framework-core,autofac,C#,.net,.net Core,Entity Framework Core,Autofac,我在windows服务中使用EF Core和Autofac上的存储库模式 我有一个服务需要连接十几个数据库,这些数据库具有相同的模式(相同的dbcontext),但只有不同的数据。 如何使用Autofac在我的服务中实现这一点?贝洛 public class ReportRepository : IReportRepository { private readonly ReportDbContext dbContext; public ReportRep
public class ReportRepository : IReportRepository
{
private readonly ReportDbContext dbContext;
public ReportRepository(ReportDbContext dbContext)
{
this.dbContext = dbContext
}
public SomeModel GetData()
{
return dbContext.SalesData;
}
}
public class ReportService : IReportService
{
private readonly IReportRepository reportRepositoryEUServer;
public ReportService(IReportRepository reportRepositoryEUServer)
{
this.reportRepositoryEUServer = reportRepositoryEUServer
}
public SomeModelDto GenerateReport()
{
var euData = reportRepositoryEUServer.GetData();
// I need to call other servers (e.g LATAM) here and get the data and aggregate them with euData
}
}
创建基本上下文,包括所有设置、数据库集等:
public abstract class BaseContext : DbContext
{
public BaseContext(DbContextOptions options)
: base(options)
{ }
public DbSet<object> FirstSet { get; set; }
...
}
并在Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddDbContext<LATAMContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LATAMConnectionString")));
services.AddDbContext<EUContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EUConnectionString")));
// Autofac
var builder = new ContainerBuilder();
// needed only if you plan to inject ICollection<BaseContext>
builder.RegisterType<LATAMContext>().As<BaseContext>();
builder.RegisterType<EUContext>().As<BaseContext>();
builder.Populate(services);
return new AutofacServiceProvider(builder.Build());
}
现在您可以注入这两种上下文
public class ReportRepository : IReportRepository
{
private readonly LATAMContext latamDbContext;
private readonly EUContext euDbContext;
public ReportRepository(LATAMContext latamDbContext, EUContext euDbContext)
{
this.latamDbContext = latamDbContext;
this.euDbContext = euDbContext;
}
}
或者如果您计划注入上下文集合
public class ReportRepository : IReportRepository
{
private readonly ICollection<BaseContext> dbContexts;
public ReportRepository(ICollection<BaseContext> dbContexts)
{
this.dbContexts = dbContexts;
}
}
你的解决方案看起来不错,但不是我想要的。我只是给出了两个连接的示例。我有几十个这样的,硬编码会很糟糕。我会更新这个问题,因为一开始我的错误是没有提到这一点。检查:应该符合你的要求needs@Greg这是否适用于代码优先实体框架数据库迁移?@MbusoMkhize您可以指定用于迁移的上下文
--context MyDbContext
您是否检查了autofac文档中的装饰程序
public class ReportRepository : IReportRepository
{
private readonly LATAMContext latamDbContext;
private readonly EUContext euDbContext;
public ReportRepository(LATAMContext latamDbContext, EUContext 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 _euContext = dbContexts.FirstOrDefault(x => x is EUContext) as EUContext;
var _latamContext = dbContexts.FirstOrDefault(x => x is LATAMContext) as LATAMContext;