c# - 多数据库登录
问题描述
我知道这是一个重复的问题,即使在这里,但我已经尝试了所有没有意义的解决方案。我有主数据库,用户检查是否找到并由他自己或相关的数据库返回,该数据库应该将连接字符串更改为当前使用的。我的问题是,尽管它的数据库是正确的,但是它dbcontext
在主数据库上工作,它再次转移我不知道为什么。
我applicationdbcontext
的是
public ApplicationDbContext(string connectionString)
: base(string.IsNullOrEmpty(connectionString) ? "DefaultConnection" : connectionString, throwIfV1Schema: false)
{
this.Database.CommandTimeout = 600;
}
public static ApplicationDbContext Create(string dbCatlogConn)
{
return new ApplicationDbContext(ConString.dbCatlogConn);
}
这是我的公开课
public class ConString
{
public static string dbCatlogConn { get; set; }
}
这是我在 accountcontroller 类中的登录信息
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl, string language = Config.DefaultLanguage)
{
try
{
System.Web.Helpers.AntiForgery.Validate();
}
catch
{
return RedirectToRoute("Login", new { language, returnUrl });
}
dbName = model.username;
Session["dbName"] = dbName;
var dbname = db.SchoolLists
.Where(t => (t.dbname == dbName))
.Select(t => new { ConnString = t.ConnectionString }).ToList();
// new conection
dbConnectionString = "";
Session["ConnectionString"] = dbConnectionString;
db = new ApplicationDbContext();
UserManager.PasswordHasher = new CustomPasswordHasher();
bool CheckConnResult = db.Database.Exists();
// code here
var user = db.Users.Where(e => e.UserName.ToLower() == model.UserName.ToLower()).FirstOrDefault();
var result = new SignInStatus();
if (user == null)
result = SignInStatus.Failure;
else
{
string dbPassword = dal.DecryptPassword(user.AnotherUsername, user.AnotherSalt, user.PasswordHash);
var status = UserManager.PasswordHasher.VerifyHashedPassword(dbPassword, model.Password);
if (status == PasswordVerificationResult.Success)
// error here
result = await SignInManager.PasswordSignInAsync(model.UserName, user.PasswordHash, model.RememberMe, shouldLockout: false);
// result = SignInStatus.Success;
else
result = SignInStatus.Failure;
}
switch (result)
{
case SignInStatus.Success:
if (user != null)
{
if (user.Disabled == true)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
ModelState.AddModelError("", language == "Invalid login attempt.");
// rest the connection to default
// = ConString.Mainbd;
return View(model);
//return View("Lockout");
}
else
{
}
}
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", language == "Invalid login attempt.");
return View(model);
}
}
我也尝试applicationdbcontext
在启动文件中更改创建
app.CreatePerOwinContext(() => ApplicationDbContext.Create(ConString.dbCatlogConn));
但ApplicationSignInManager
始终使用主连接 - 尽管我正确获取了连接字符串,但登录后它不会更新新连接
解决方案
您可以尝试通过创建如下配置吗?然后动态更改连接字符串。
public static class ApplicationDbContextConfigurer
{
public static void Configure(
DbContextOptionsBuilder<ApplicationDbContext> builder, string connectionString)
{
builder.UseSqlServer(connectionString);
}
public static void Configure(
DbContextOptionsBuilder<ApplicationDbContext> builder, DbConnection connection)
{
builder.UseSqlServer(connection);
}
}
更改连接字符串
DbContextOptionsBuilder<ApplicationDbContext> builder =
new DbContextOptionsBuilder<VineforceDbContext>();
ApplicationDbContextConfigurer.Configure(builder, connectionString);
推荐阅读
- node.js - 为快速静态中间件指定静态文件路径的正确方法
- reactjs - 有没有办法在 Electron 应用程序中嵌入 React Native 模拟器?
- imagemagick - ImageMagick:删除 2 像素宽的线条
- android - 如何在半透明的StatusBar和ActionBar之间进行无缝过渡?
- javascript - 系统自动化 = 网站仪表板 + 移动应用程序和 Wordpress
- android - 模拟器“将不加速运行”和 qemu 系统错误
- excel - excel activex文本框不出现滚动条
- python - 尝试初始化环境时出错(Pipenv))
- php - Laravel 和干预图像的“未定义索引”错误
- javascript - 未选中父项时,尝试清除子项复选框