Reputation: 11
In my C# ASP.NET MVC project, I have an issue where the database manager switches to the wrong database. This happens if more than one session is running and the two sessions are using different databases.
The crazy thing is it's not happening every time. Specifically I'm using the Kendo scheduler and after booking about 4 appointments, it'll book one of them in a completely different database than the one I am in.
Does anyone have ANY clue as to what might be happening?
public class DBManager
{
private DBContextContainer Database { get; set; }
private PSIUsersEntities UsersDB { get; set; }
public DBManager()
{
Database = new DBContextContainer();
USERPATH usr = UserUtil.GetCurrentUser();
if (usr != null)
{
Debug.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$: " + usr.DBNAME);
this.ChangeDatabase(usr.DBNAME);
}
UsersDB = new PSIUsersEntities();
}
private void ChangeDatabase(string databaseName)
{
Database.ChangeDatabase(initialCatalog: databaseName);
}
[ThreadStatic]
private static DBManager _current;
public static DBManager Current
{
get
{
if (_current == null)
{
_current = new DBManager();
}
return _current;
}
}
public enum LoginResult
{
Error,
IncorrectPassword,
IncorrectUser,
Success
}
public LoginResult LogIn(string user, string password)
{
Debug.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ user : " + user);
USERPATH dbUser = UsersDB.USERPATHs.FirstOrDefault(usr => usr.USERNAME == user);
LoginResult result;
if (dbUser != null)
{
Debug.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~dbuser: " + dbUser);
Debug.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~usedbName: " + dbUser.DBNAME);
result = LoginResult.Success;
UserUtil.SetCurrentUser(dbUser);
ChangeDatabase(dbUser.DBNAME);
}
else
{
return LoginResult.IncorrectUser;
}
return result;
}
}
using ConfigUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using UpgradeHelpers.Interfaces;
using System.Diagnostics;
Upvotes: 1
Views: 26