[英]Use different Database, with different DBcontext, and connect to it according to different URL request
we actually have an online app, which include a ChatBot .我们实际上有一个在线应用程序,其中包括一个ChatBot 。
This chatbot can show us different informations from a database, like这个聊天机器人可以向我们展示来自数据库的不同信息,比如
Me: "What is the estimated budget for Projcet1"
Chat bot: "The estimated budget is 50k € for Project1"
Fine.美好的。 Now, we will deploy this application for different companies.
现在,我们将为不同的公司部署这个应用程序。 And each company have a different Database.
每个公司都有不同的数据库。
For those different Database (2 at the moment) I created 2 context.对于那些不同的数据库(目前为 2 个),我创建了 2 个上下文。 I have 2 models to interact with those database, and I can add Controller for every models.
我有 2 个模型可以与这些数据库交互,我可以为每个模型添加 Controller。
services.AddDbContext<data_firstContext>(options =>
{
options.UseMySQL(Configuration.GetConnectionString("Database1"));
});
services.AddDbContext<data_secondContext>(options =>
{
options.UseMySQL(Configuration.GetConnectionString("Database2"));
});
The question now is, how can I chose to connect either to the first database, or to the second database.现在的问题是,我如何选择连接到第一个数据库或第二个数据库。 I can send from the front-end, the URL header of the App or something like that, or just catch it somewhere I guess, but how can I switch from one to an other depending on the Url request, or something I give in parameter from the front-end.
我可以从前端发送应用程序的 URL header 或类似的东西,或者只是在我猜的某个地方捕捉它,但是我如何根据 Z02A3A357701 参数中的 Z02A3A357701 参数0CC2A5DFDFB74 请求或给出的东西从一个切换到另一个从前端。 I looked online but none of what I've tried actually work.
我在网上看了,但我尝试过的都没有真正奏效。 If someone have an idea, it'd really help me !
如果有人有想法,那真的会帮助我! Thanks
谢谢
Assuming you are using EF core, you don't have to pass any parameter as such from the UI.假设您使用的是 EF 核心,则不必从 UI 传递任何参数。 You can do that directly by defining different
DBContext
s and use them in the constructors
of your services by using Dependency Injection
.您可以通过定义不同的
DBContext
直接做到这一点,并通过使用Dependency Injection
在服务的constructors
中使用它们。 For eg:例如:
Startup.cs:启动.cs:
services.AddDbContext<data_firstContext>(options =>
{
options.UseMySQL(Configuration.GetConnectionString("Database1"));
});
services.AddDbContext<data_secondContext>(options =>
{
options.UseMySQL(Configuration.GetConnectionString("Database2"));
});
FirstDbContext.cs: FirstDbContext.cs:
public class FirstDbContext : DbContext
{
public DbSet<ViewModels.Tower> Towers { get; set; }
public DbSet<ViewModels.Motherboard> Motherboards { get; set; }
public FirstDbContext(DbContextOptions<FirstDbContext> options)
: base(options)
{
}
}
AdminController.cs: AdminController.cs:
[Authorize(Policy = "RequireAdminRole")]
public class AdminController : Controller
{
private readonly FirstDbContext _context;
public AdminController(FirstDbContext context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
public IActionResult Towers()
{
var model = _context.Towers.ToList();
return View(model);
}
}
Check this out: Entity Framework Core Using multiple DbContexts看看这个: Entity Framework Core Using multiple DbContexts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.