简体   繁体   English

在 .NET 5 Web API 中动态连接到多个数据库(取决于经过身份验证的用户)

[英]Connecting to multiple databases, dynamically (depending on the authenticated user) in an .NET 5 Web API

We currently have a CMS that our customers can use to manage the content and certain setting of the websites we make for them.我们目前有一个 CMS,我们的客户可以使用它来管理我们为他们制作的网站的内容和某些设置。 We have one main database that contains the list of all customers.我们有一个包含所有客户列表的主数据库。 Each customer then has their own database, which contains all their data.然后,每个客户都有自己的数据库,其中包含他们的所有数据。 We always connect to the main database first, then lookup the customer and use that info to connect to the database of the customer and get their data.我们总是先连接到主数据库,然后查找客户并使用该信息连接到客户的数据库并获取他们的数据。

We are working on remaking our CMS (and it's API) in .NET 5. We use dependency injection for all our services and for the database connection.我们正在努力在 .NET 5 中重建我们的 CMS(及其 API)。我们对所有服务和数据库连接使用依赖注入。 At the moment we have an IDatabaseConnection with the implementations MySqlDatabaseConnection and MsSqlConnection .目前我们有一个IDatabaseConnection实现MySqlDatabaseConnectionMsSqlConnection This has some functions for executing queries, starting transactions etc. The constructor has an IOptions<MySettings> to inject the settings from the appSettings.json , which contains the connection string.这有一些用于执行查询、启动事务等的功能。构造函数有一个IOptions<MySettings>来注入来自appSettings.json的设置,其中包含连接字符串。

This works fine for the main database, but we are trying to find out the best way to connect to the customer's database, since the connection strings for those databases come from the main database, we don't know them beforehand.这适用于主数据库,但我们正在尝试找出连接到客户数据库的最佳方式,因为这些数据库的连接字符串来自主数据库,我们事先并不知道它们。

We have been thinking about creating a factory for this, but not sure how to do that in this specific case.我们一直在考虑为此创建一个工厂,但不确定在这种特定情况下如何做到这一点。 Another thing we found is that we implement multi tenancy, but also not sure if that is the best way.我们发现的另一件事是我们实现了多租户,但也不确定这是否是最好的方式。 We can also just add an empty constructor to our implementations and manually create instances and set the correct connection string when we need them, but that seems to be an anti-pattern?我们也可以在我们的实现中添加一个空的构造函数,并在需要时手动创建实例并设置正确的连接字符串,但这似乎是一种反模式? Or is it somehow possible to inject 2 instances of our IDatabaseConnection and then set the connection string manually for one of them?或者是否有可能注入我们的IDatabaseConnection的 2 个实例,然后为其中一个手动设置连接字符串?

When we look for information online, we mostly come across solutions for Entity Framework or other object database mappers or by using DbContext, but as far as I know we can't use anything like that, because our CMS also has dynamic queries and things like that (I can explain more about that if you need more context).当我们在线查找信息时,我们大多会遇到实体框架或其他 object 数据库映射器或使用 DbContext 的解决方案,但据我所知,我们不能使用类似的东西,因为我们的 CMS 也有动态查询和类似的东西那(如果您需要更多上下文,我可以对此进行更多解释)。

So do you have any suggestions/recommendations for handling a use case like this?那么您对处理这样的用例有什么建议/建议吗? Do you need any code samples of what we have so far?您需要我们目前所拥有的任何代码示例吗?

I had the same problem a few days ago.几天前我遇到了同样的问题。

You can change the connectionstring on the fly by this:您可以通过以下方式即时更改连接字符串:

myDbContext.Database.GetDbConnection().ConnectionString = $@"Data Source=.\SqlExpress;Initial Catalog={dataasename};Integrated Security=True";

If you want to use custom connectionstring you can create the DbContext like this:如果要使用自定义连接字符串,可以像这样创建 DbContext:

public static MyDbContext GetMyDbContext(string databaseName)
{
    var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();

    optionsBuilder.UseSqlServer($@"Data Source=.\SqlExpress;Initial Catalog={databaseName};Integrated Security=True");

    return new MyDbContext(optionsBuilder.Options);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从.Net Web API中经过Azure身份验证的AD获取用户名 - Get user name from Azure authenticated AD in .Net Web API 连接到MVC Web应用程序中的多个数据库 - Connecting to multiple databases in MVC web application 如何在Web API 2中检查用户是否已通过身份验证 - How to check if user is authenticated in Web API 2 经过身份验证的用户在 web api owin 中间件中丢失 - Authenticated user is lost in web api owin middleware 用户通过身份验证时调用Web Api方法 - Calling a Web Api method when user is authenticated 如何从 asp.net web api 中的身份声明获取 JWT 身份验证的登录用户 - How to get JWT authenticated sign-in user from identity claims in asp.net web api ASP.NET Core Web API and MongoDB with multiple Collections and multiple Databases - ASP.NET Core Web API and MongoDB with multiple Collections and multiple Databases ASP.NET Core Web API - 使用Sybase和SQL Server实现多数据库 - ASP.NET Core Web API - implement multiple databases using Sybase and SQL Server 具有相同登录名的多个数据库(.net core 3 API) - Multiple databases with same login (.net core 3 API) 注入的IPrincipal是匿名的,但Web API Controller User对象已通过身份验证 - Injected IPrincipal is anonymous but Web API Controller User object is authenticated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM