1

we actually have an online app, which include a 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. I have 2 models to interact with those database, and I can add Controller for every models.

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. I looked online but none of what I've tried actually work. If someone have an idea, it'd really help me ! Thanks

Moost_
  • 41
  • 3
  • How these data contexts are currently used by the application? – Chetan Dec 17 '20 at 14:27
  • From my point of view, is it possible to deploy different applications for different companies? I mean that, different companies may have individual requirements in the future. That means you may provide different databases with different tables and different source code to provide commercial service. If it's possible, each company will have their specific server, application, database and request url. If this can't achieve, with your idea, trying to add company info(userId, companyId...) into your request parameters. Please don't blame if I misunderstand your mean. – Tiny Wang Dec 17 '20 at 15:01
  • Maybe this will help: [Implementing database per tenant strategy](https://www.codingame.com/playgrounds/5518/multi-tenant-asp-net-core-5---implementing-database-per-tenant-strategy) – Peter B Dec 17 '20 at 16:14
  • You're all right @Tiny-wa . We already have different application, and different back-end for each clients. But we would like to have only one "chatbot server" for every customer, as it is doing the same thing for every clients. – Moost_ Dec 17 '20 at 17:29
  • @ChetanRanpariya they are used through our ChatBot, to get some informations (Budget Price, Hours Needed on a project, Number of tenders for the company...). – Moost_ Dec 17 '20 at 17:29
  • @PeterB yep thanks, I already saw that and I tried to adapt it to our code. Maybe I'll look more deeply into it because for now that doesn't work with what we do – Moost_ Dec 17 '20 at 17:29
  • @Moost_ Thank you. In that case, how about creating callback-interface in each application for your chatbot? I.e. if company1 -> app1 -> bot server, company2 -> app2 ->bot server, in this case, I think you can create callback-interface and it will be companyX -> appX -> botserver -according to the requestMessage-> appX -> companyX. – Tiny Wang Dec 18 '20 at 01:28

1 Answers1

0

Assuming you are using EF core, you don't have to pass any parameter as such from the UI. You can do that directly by defining different DBContexts and use them in the constructors of your services by using Dependency Injection. For eg:

Startup.cs:

services.AddDbContext<data_firstContext>(options =>
            {       
                options.UseMySQL(Configuration.GetConnectionString("Database1"));

            });

services.AddDbContext<data_secondContext>(options =>
                {       
                    options.UseMySQL(Configuration.GetConnectionString("Database2"));
    
                });

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:

[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

Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • I saw the link you send earlier indeed. But I can't see where we specify to check Database#1 or Database#2 . I don't understand how can I "orient" a question, in the Chatbot, from Website#1 or Website#2 . All request are for the moment looking into Database#1 – Moost_ Dec 17 '20 at 17:41
  • Have different controllers for different answers. For example: If the user answers `A`, you will call `FirstDbContextController`, if user answers `B`, you will call `SecondDbContextController`. – Harshita Singh Dec 18 '20 at 06:42