C# 如何从Blazor.NET核心项目连接到多个数据库?
如何连接到Blazor WebAssembly项目中的多个数据库实例,在该项目中我还添加了ASP.NET核心托管 我的想法是将C# 如何从Blazor.NET核心项目连接到多个数据库?,c#,blazor,blazor-server-side,C#,Blazor,Blazor Server Side,如何连接到Blazor WebAssembly项目中的多个数据库实例,在该项目中我还添加了ASP.NET核心托管 我的想法是将DBContexts启动到'Startup.cs`(来自Blazor.Server应用程序,该应用程序引用了Blazor.Client应用程序): public void配置服务(IServiceCollection服务) { services.AddDbContext(选项=> options.UseSqlite( “连接管柱支架…”); } 像这样,但是我想让用户在
DBContexts
启动到'Startup.cs`(来自Blazor.Server应用程序,该应用程序引用了Blazor.Client应用程序):
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlite(
“连接管柱支架…”);
}
像这样,但是我想让用户在我的视图中选择是否要在其中创建SQLite数据库实例的应用程序上进行测试运行。常规运行将是SQL Server数据库的一个实例。如何在ConfigureServices方法中执行此操作
现在我正在构建DBContexts
类,这些类是否也受到影响
控制器尚未完成,ASP.NET核心MVC控制器是正确的选择吗?我还没有尝试过,但您可以尝试类似的方法- 在Blazor客户端中
Program.cs
-
builder.Services.AddDatabaseStorage();
在Blazor服务器的Startup.cs中
public void ConfigureServices(IServiceCollection services)
{
services.AddDatabaseStorage();
}
创建一个静态类并添加所有DB存储接口及其实现-
public static class StorageServiceCollections
{
public static IServiceCollection AddDatabaseStorage(this IServiceCollection
services)
{
//you may also return DB service based on certain condition here. Like factory, singleton pattern.
return services
.AddSingleton<IDBStorageService, SQLStorageService>()
.AddSingleton<IMongoDBStorageService, MongoStorageService>();
}
}
注入razor组件以调用它们-
@inject IDBStorageService dbStorage
我还没试过,但你可以试试这样的- 在Blazor客户端中
Program.cs
-
builder.Services.AddDatabaseStorage();
在Blazor服务器的Startup.cs中
public void ConfigureServices(IServiceCollection services)
{
services.AddDatabaseStorage();
}
创建一个静态类并添加所有DB存储接口及其实现-
public static class StorageServiceCollections
{
public static IServiceCollection AddDatabaseStorage(this IServiceCollection
services)
{
//you may also return DB service based on certain condition here. Like factory, singleton pattern.
return services
.AddSingleton<IDBStorageService, SQLStorageService>()
.AddSingleton<IMongoDBStorageService, MongoStorageService>();
}
}
注入razor组件以调用它们-
@inject IDBStorageService dbStorage
您可以使用2个DB上下文、一个接口和一个从API请求中发送的数据中选择上下文的服务来实现: DB上下文接口
公共接口IDatabaseContext
{
//在此处添加所有DbSet声明
}
db上下文
public class DatabaseContext : IDatabaseContext
{
// db context implementation
}
测试数据库上下文
public class DatabaseContext : IDatabaseContext
{
// db context implementation
}
公共类TestDatabaseContext:DatabaseContext
{
//添加构造函数
}
DbContext解析器服务
公共类DbContextResolver
{
公共bool IsTest{get;set;}
服务器端DI设置
services.AddDbContext(选项=>
options.UseSqlServer(
“SqlServer连接字符串保持器…”)
.AddDbContext(选项=>
options.UseSqlite(
“Sqlite连接字符串保持器…”)
.AddScoped())
.addScope(p=>
{
var resolver=p.GetRequiredService();
if(解析器.IsTest)
{
重新运行p.GetRequiredService();
}
返回p.GetRequiredService();
}));
从请求中选择数据库上下文
public class DatabaseContext : IDatabaseContext
{
// db context implementation
}
public void配置(IApplicationBuilder应用程序)
{
应用程序使用((上下文,下一步)=>
{
var resolver=context.RequestServices.GetRequiredService();
resolver.IsTest=context.Request.Query.ContainsKey(“test”);//此处上下文由查询字符串选择,但您可以选择发送头
返回next();
}
}
在控制器或服务中使用所选的DB上下文
公共类MyController:Controller
{
公共MyController(IDatabaseContext上下文)
...
}
您可以使用2个DB上下文、一个接口和一个从API请求中发送的数据中选择上下文的服务来实现:
DB上下文接口
公共接口IDatabaseContext
{
//在此处添加所有DbSet声明
}
db上下文
public class DatabaseContext : IDatabaseContext
{
// db context implementation
}
测试数据库上下文
public class DatabaseContext : IDatabaseContext
{
// db context implementation
}
公共类TestDatabaseContext:DatabaseContext
{
//添加构造函数
}
DbContext解析器服务
公共类DbContextResolver
{
公共bool IsTest{get;set;}
服务器端DI设置
services.AddDbContext(选项=>
options.UseSqlServer(
“SqlServer连接字符串保持器…”)
.AddDbContext(选项=>
options.UseSqlite(
“Sqlite连接字符串保持器…”)
.AddScoped())
.addScope(p=>
{
var resolver=p.GetRequiredService();
if(解析器.IsTest)
{
重新运行p.GetRequiredService();
}
返回p.GetRequiredService();
}));
从请求中选择数据库上下文
public class DatabaseContext : IDatabaseContext
{
// db context implementation
}
public void配置(IApplicationBuilder应用程序)
{
应用程序使用((上下文,下一步)=>
{
var resolver=context.RequestServices.GetRequiredService();
resolver.IsTest=context.Request.Query.ContainsKey(“test”);//此处上下文由查询字符串选择,但您可以选择发送头
返回next();
}
}
在控制器或服务中使用所选的DB上下文
公共类MyController:Controller
{
公共MyController(IDatabaseContext上下文)
...
}
blazor不是MVC,不要将razor和blazor()混用关于多个数据库,这与blazor无关,请检查这一点blazor WASM应用程序中的多个DB连接无关。blazor WASM是一个UI框架,与任何前端框架一样。对于DB操作,您必须创建一个Web API并为多个DB连接创建多个DBContext。@Rahul这就是我所做的已完成“WebAssembly ASP.NET核心托管”意思是。我的项目结构现在是Blazor.Client、Blazor.Server和Blazor.Shared Lib。所以现在我在服务器项目中有了Startup.cs,我将把控制器放在其中。为什么多重数据库连接不可能?我还不明白。@maytham-ɥɥʇʎɯ我不能在Blazor.Server项目中这样做吗?它是在我创建时创建的在创建之前,请检查ASP.NET Core Hosted选项