我开发了一个简单的网络应用程序,并且在将来,我希望将其作为多租户进行。
所以我想直接将连接字符串写入204
方法:
OnConfiguring
启动课程:
public class ApplicationContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connection string from appsettings.json");
base.OnConfiguring(optionsBuilder);
}
}
如何从public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationContext>();
services.AddMvc();
}
提取连接字符串到appsettings.json
类?
我不想为ApplicationContext
类创建任何构造函数。
答案 0 :(得分:12)
让我们假设您拥有 .NET Core 应用程序,而您的appsettings.json
文件如下所示:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Production": {
"SqliteConnectionString": "Filename=./MyDatabase.sqlite"
}
}
您可以从SqliteConnectionString
获取Startup.cs
这样的值:
public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration["Production:SqliteConnectionString"];
services.AddDbContext<MyContext>(options =>
options.UseSqlite(connection)
);
....
}
然后在DBContext
中,您应该添加接受DbContextOptions
的构造函数:
public class MyContext : DbContext
{
public MyContext (DbContextOptions<MyContext> options) : base(options)
{ }
...
}
答案 1 :(得分:5)
所以我想直接将连接字符串写入OnConfiguring方法:
如何从appsettings.json中提取连接字符串到ApplicationContext类?
我不想为ApplicationContext类创建任何构造函数。
您可以通过IOptions
使用选项模式,但最简单的方法是在ApplicationContext
构造函数中使用DI;)
请按照以下文章:
答案 2 :(得分:4)
.NET Core 2.0
添加此课程:
// Requires NuGet package Microsoft.Extensions.Configuration.Json
using Microsoft.Extensions.Configuration;
using System.IO;
namespace RutarBackgroundServices.AppsettingsJson
{
public static class AppSettingsJson
{
public static string ApplicationExeDirectory()
{
var location = System.Reflection.Assembly.GetExecutingAssembly().Location;
var appRoot = Path.GetDirectoryName(location);
return appRoot;
}
public static IConfigurationRoot GetAppSettings()
{
string applicationExeDirectory = ApplicationExeDirectory();
var builder = new ConfigurationBuilder()
.SetBasePath(applicationExeDirectory)
.AddJsonFile("appsettings.json");
return builder.Build();
}
}
}
从“appsettings.json”文件中获取键“MssqlConnectionString”的值:
var appSettingsJson = AppSettingsJson.GetAppSettings();
var connectionString = appSettingsJson["MssqlConnectionString"];
在项目的根目录中创建文件“appsettings.json”:
{
"MssqlConnectionString": "Server=yourip; Database=yourdbname; User Id=yourusername; Password=yourpassword; Pooling=true;",
"Db2ConnectionString": "Database=yourdbname;UserID=yourusername;Password=yourpassword;Server=yourip:yourport",
"SomeOtherKey": "SomeOtherValue"
}
答案 3 :(得分:0)
您可以使用工厂模式解析DbContext
。
public interface ITenantDbContextFactory
{
ApplicationContext Create(string tenantId);
}
public class TenantDbContextFactory()
{
private ApplicationContext context;
public TenantDbContextFactory()
{
}
public ApplicationContext Create(string tenantId)
{
if(this.context==null)
{
var connectionString = GetConnectionForTenant(tenantId);
var dbContextBuilder = new DbContextOptionsBuilder();
dbContextBuilder.UseSqlServer(connectionString);
this.context = new ApplicationContext(dbContextBuilder);
}
return this.context;
}
}
在初创公司:
services.AddDbContext<ApplicationContext>();
services.AddScoped<ITenantDbContextFactory, TenantDbContextFactory>();
您的服务或控制人员:
public class HomeController
{
private readonly ITenantDbContextFactory dbFactory;
public HomeControler(ITenantDbContextFactory tenantDbContextFactory)
{
this.dbFactory = tenantDbContextFactory;
}
public void Action()
{
var dbContext = this.dbFactory.Create("tenantA");
// use your context here
dbContext...
}
}