mardi 2 avril 2019

How to change the database at runtime

I have a WEB API that starts with a security database, the user's login, returns ConnectionStringsName and, for each request, validates the ConnectionStringsName connection that is in web.config for the user

    namespace AccesoDatos
    {
        public  class DaoConexion: MarshalByRefObject
        {
            public Database DB { get; set; }
            //public Database DaoConexionDbContext(string connectionStringName ="DefaultConnection2")
            public Database DaoConexionDbContext()
            {
                var claims = ((ClaimsIdentity)Thread.CurrentPrincipal.Identity);
                var claim = claims.Claims.Where(x => x.Type == "ConnectionStringsName").FirstOrDefault();

                if (claim ==null)
                {
                    string connectionStringName= "DefaultConnection";
//here works well

                    return DB = DatabaseFactory.CreateDatabase(connectionStringName);
                }
//error The requested database DefaultConnection1 is not defined in configuration
                return DB = DatabaseFactory.CreateDatabase(claim.Value);            
            }       
        }
    }




Aucun commentaire:

Enregistrer un commentaire