我正在向我的 Web 应用程序添加一些“租赁”。
我的 Web 应用程序使用 2 个数据库
- 身份数据库
- 一个特定于租户的数据库(所有相同的架构)
该应用程序是使用 一个静态 连接字符串构建的,用于第二个 数据库。
我使用静态连接字符串运行良好的 AppointmentsControllers:
public class AppointmentsController : Controller
{
private appDbContext _context;
public AppointmentsController()
{
_context = new appDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// Index
public ViewResult Index()
{
var query = from c in _context.Appointments
orderby c.RegistrationDate
select c;
//etc...
}
}
为了添加一些 Multi-Tenancy ,我更改了 DbContext 部分,以便我可以修改数据库连接。 这部分工作正常,它允许我更改数据库名称:
public class appDbContext : DbContext
{
public DbSet<Appointments> Appointments { get; set; }
public appDbContext(string database)
: base(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=" + database + ";Integrated Security=True")
{
}
}
从这里我迷路了。似乎可能从我的 AppointmentsController 构造函数连接正确的数据库,如下所示:
public class AppointmentsController : Controller
{
private appDbContext _context;
public AppointmentsController()
{
_context = new appDbContext("DbName");
}
但是我不能从身份中检索我的 Claim 并将它从这个构造函数传递给构造函数,如下所示:
public class AppointmentsController : Controller
{
private appDbContext _context;
public AppointmentsController()
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindById(User.Identity.GetUserId());
_context = new appDbContext(currentUser.DBName);
}
所以我完成工作的最佳方法是这样,但我认为它不是很干净/流畅:
public class AppointmentsController : Controller
{
private appDbContext _context;
public AppointmentsController()
{
_context = new appDbContext("");
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
private string GetCurrentUserDatabaseName()
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindById(User.Identity.GetUserId());
return currentUser.DBName;
}
// Index
public ViewResult Index()
{
_context = new appDbContext(GetCurrentUserDatabaseName());
var query = from c in _context.Appointments
orderby c.RegistrationDate
select c;
//etc
}
}
我觉得我很接近,但我该如何改进这段代码?
最佳答案
在 Web.Config
中添加两个连接字符串:
<connectionStrings>
<add name="DefaultConnection" connectionString=Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DefaultDataBase;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
然后
public class ApplicationDbContext : DbContext //change DbContext name appDbContext by ApplicationDbContext
{
public DbSet<Appointments> Appointments { get; set; }
public ApplicationDbContext(database=""): base("name=DefaultConnection")
{
//And/Or you can do this programmatically.
if(!string.IsNullOrWhiteSpace(database)
{
this.Database.Connection.ConnectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=" + database + ";Integrated Security=True";
//OR Database.Connection.ChangeDatabase(database);
}
// More Stuff.....
}
}
使用:
public class AppointmentsController : Controller
{
private ApplicationDbContext _context;
public AppointmentsController()
{
_context = new ApplicationDbContext("DataBasConnection");
}
}
或
当你需要 DefultDatabase 时
public class AppointmentsController : Controller
{
private ApplicationDbContext _context;
public AppointmentsController()
{
_context = new ApplicationDbContext();
}
}
关于c# - 基于身份声明的动态数据库连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47423682/