简体   繁体   English

使用不同的Database,有不同的DBcontext,根据不同的URL请求连接到它

[英]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.

相关问题 不同DbContext的UnitOfWork - UnitOfWork for different DbContext 代码优先生成的数据库与DbContext模型不同 - Code-First generated database different from DbContext Model DataAccesLayer DbContext中用于不同环境的不同数据库 - Different databases for different environments in DataAccesLayer DbContext 有不同的连接字符串时如何使用构造函数来实现 DbContext? - How to use constructor to implement DbContext when have different connection string? 在不同的DbContext实例上重新执行IQueryable? - Reexecuting IQueryable on different DbContext instance? 在MVC 5中推荐的方法,以将Applicationuser用作不同DBContext中的Navigation属性 - Recommended approach in MVC 5 to use Applicationuser as Navigation property in different DBContext 如何使用来自不同程序集的 DbContext 进行迁移? - How can I use a DbContext from a different assembly for my migrations? 是否可以创建 DbContext 接口或抽象类并使用它来注入不同的 DbContext 对象? - Is it possible to create a DbContext Interface or abstract class and use it to inject different DbContext Objects? 作弊应用程序连接到不同的SQL Server数据库 - Cheat application to connect to different SQL Server database 如何在.NET Core中连接到不同类型的数据库 - How to connect to different type of database in .NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM