Using static management class to control db transactions through an EF6
umm yes, you could do it that way.


  as far as I understood that EF context is not thread-safe so I'm
  instantiating one with every call through the using statement


Where did you read this?  I'm not denying it it's just that I have not read this before and I haven't experienced too many problems with using EF in the case of one instance per request.


  My second question, is the use of Boolean variable ok to signal
  failure in db transactions


Maybe.  But how are you going to know what actually went wrong?  All that's going to happen is you know something went wrong but when it comes to diagnosing it you will be at a dead end because you have swallowed the exception (so to speak).  Given that your exceptions seeming to be handling cases such as the database offline, or connection issues I think it would be best to bubble those up as that would be a core system fault and you would want to know more info.

Here's an alternative solution using a non-static class and sharing the Database context between methods.


  NOTE:  I would actually consider removing the default empty
  constructor and IDisposable if you were using an IOC framework to
  instantiate the context as that would be responsible for your instance
  lifetime.  However I'm not sure how your Account Manager would be created.


public class AccountManager : IDisposable
{
    private enum Role { User, Comment, CommentControl, Sysadmin }
    private readonly AccountsDbCtx dbContext;

    public AccountManager(AccountsDbCtx dbContext)
    {
        this.dbContext = dbContext;
    }

    public AccountManager() : this(new AccountsDbCtx())
    {
    }

    public void Dispose() 
    {
        dbContext.Dispose();
    {

    public bool IsUserInRole(string username, string role)
    {
        var account = dbContext.Accounts.SingleOrDefault(x =&gt; x.Username == username);

        if(account == null)
        {
            return false;
        }

        var existingRole = account.Roles.Where(x =&gt; x.Name.Equals(role, StringComparison.InvariantCultureIgnoreCase));

        return existingRole.Any();      
    }

    public bool IsUserCached(string username)
    {
        return dbContext.Accounts.Any(x =&gt; x.Username == username); 
    }

    public void CacheUser(string username)
    {
        dbContext.Accounts.Add(new Account
        {
            Active = true,
            Username = username
        });

        dbContext.Accounts.Add(newAcc);
        dbContext.SaveChanges();

        AssignRoleUser(username);       
    }

    public bool AssignRoleUser(string username)
    {
        return AssignRole(username, Role.User);
    }

    public bool AssignRoleComment(string username)
    {
        return AssignRole(username, Role.Comment);
    }

    public bool AssignRoleCommentControl(string username)
    {
        return AssignRole(username, Role.CommentControl);
    }

    public bool AssignRoleSysAdmin(string username)
    {
        return AssignRole(username, Role.Sysadmin);
    }

    private bool AssignRole(string accountName, Role role)
    {
        // Why would a role not exist in our database?
        var roleToAssign = dbContext.Roles.Single(x =&gt; x.Role1 == (int)role);               
        var currentAccount = dbContext.Accounts.SingleOrDefault(x =&gt; x.Username.Contains(accountName));

        if (currentAccount == null)
            return false;

        currentAccount.Roles.Add(roleToAssign);
        dbContext.SaveChanges();

        return IsUserInRole(accountName, role); 
    }
}