C# 如何在.net core(非强类型)中拥有和访问数量可变的连接字符串?
在ASP.NET 4.6中,web.config中有一个ConnectionString部分,您可以在其中添加无限量的连接字符串,并将其动态读取到应用程序中,或者按名称获取单个连接字符串 我看到的ASP.NET核心示例使用appsettings.json文件存储设置,然后将这些设置绑定到具有与设置名称匹配的属性的强类型对象。具有设置值的绑定对象存储在一个容器中,以便在应用程序周围注入C# 如何在.net core(非强类型)中拥有和访问数量可变的连接字符串?,c#,asp.net-core,asp.net-core-mvc,asp.net-core-1.0,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Core 1.0,在ASP.NET 4.6中,web.config中有一个ConnectionString部分,您可以在其中添加无限量的连接字符串,并将其动态读取到应用程序中,或者按名称获取单个连接字符串 我看到的ASP.NET核心示例使用appsettings.json文件存储设置,然后将这些设置绑定到具有与设置名称匹配的属性的强类型对象。具有设置值的绑定对象存储在一个容器中,以便在应用程序周围注入 我需要在appsettings.json中有一个ConnectionString列表,并允许用户在运行时(登录时
我需要在appsettings.json中有一个ConnectionString列表,并允许用户在运行时(登录时)选择数据库。我将存储用户连接到的数据库的名称作为声明。但是,我需要能够在整个应用程序中注入或以某种方式访问连接字符串列表,以便能够获取用户连接到的DB的连接字符串。此外,我还需要能够向实体框架提供连接字符串。appsettings.json在存储和访问连接字符串方面具有与web.config相同的功能
{
"ConnectionStrings": {
"SqlServerConnection" : "Server=.\\sql2012express;Database=aspnet-IdentityServer4WithAspNetIdentity;Trusted_Connection=True;MultipleActiveResultSets=true",
"SqLiteConnection": "Data\\LynxJournal.db"
},
}
这些可以通过代码访问
_config.GetConnectionString("SqliteConnection")
其中SqlliteConnection是连接字符串的名称
_config是从Startup.cs注入IConfiguration服务得到的,在Startup.cs中设置了配置
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
Environment = env;
}
public IConfigurationRoot Configuration { get; }
private IHostingEnvironment Environment { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddEntityFrameworkSqlite().AddDbContext<LynxJournalDbContext>();
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddSingleton<IConfiguration>(Configuration);
services.AddSingleton<IHostingEnvironment>(Environment);
Services = services;
}
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,可选:true,重载更改:true)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true);
builder.AddEnvironmentVariables();
Configuration=builder.Build();
环境=环境;
}
公共IConfigurationRoot配置{get;}
私有IHostingEnvironment环境{get;}
public void配置服务(IServiceCollection服务)
{
services.AddMemoryCache();
services.AddEntityFrameworkSqlite().AddDbContext();
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddSingleton(配置);
服务。AddSingleton(环境);
服务=服务;
}
appsettings.json在存储和访问连接字符串方面具有与web.config相同的功能
{
"ConnectionStrings": {
"SqlServerConnection" : "Server=.\\sql2012express;Database=aspnet-IdentityServer4WithAspNetIdentity;Trusted_Connection=True;MultipleActiveResultSets=true",
"SqLiteConnection": "Data\\LynxJournal.db"
},
}
这些可以通过代码访问
_config.GetConnectionString("SqliteConnection")
其中SqlliteConnection是连接字符串的名称
_config是从Startup.cs注入IConfiguration服务得到的,在Startup.cs中设置了配置
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
Environment = env;
}
public IConfigurationRoot Configuration { get; }
private IHostingEnvironment Environment { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddEntityFrameworkSqlite().AddDbContext<LynxJournalDbContext>();
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddSingleton<IConfiguration>(Configuration);
services.AddSingleton<IHostingEnvironment>(Environment);
Services = services;
}
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,可选:true,重载更改:true)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true);
builder.AddEnvironmentVariables();
Configuration=builder.Build();
环境=环境;
}
公共IConfigurationRoot配置{get;}
私有IHostingEnvironment环境{get;}
public void配置服务(IServiceCollection服务)
{
services.AddMemoryCache();
services.AddEntityFrameworkSqlite().AddDbContext();
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddSingleton(配置);
服务。AddSingleton(环境);
服务=服务;
}
最简单的方法是在需要注入数据库并从中创建数据库的任何地方使用factory模式。然后为DbContext注册工厂:services.AddScoped(provider=>provider.GetService().Create())代码>没有时间做更详细的回答最简单的方法是在需要注入数据库并从中创建数据库的任何地方使用factory模式。然后为DbContext注册工厂:services.AddScoped(provider=>provider.GetService().Create())代码>没有时间获得更详细的答案