Search code examples
asp.net-mvcasp.net-identitydatabase-connectionconnection-stringidentity

Identity can't connect to remote DB, keep connecting to local DB


I'm working with Identity framework and ASP.NET MVC5 (DB First).
Everything is OK locally, but Identity can't connect to my remote DB hosted on Azure.

My connection strings in web.config looks like this

<!-- Local DB -->
<add name="MyEntities" 
    connectionString="metadata=res://*/EntityDataModel.csdl|res://*/EntityDataModel.ssdl|res://*/EntityDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MY-PC\SQLEXPRESS;Initial Catalog=MyLocalDBName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
    providerName="System.Data.EntityClient" />

<!-- Remote DB -->
<add name="MyEntities" 
     connectionString="metadata=res://*/EntityDataModel.csdl|res://*/EntityDataModel.ssdl|res://*/EntityDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:MyServerName.database.windows.net,1433;initial catalog=MyRemoteDBName;persist security info=False;user id=MyID;password=MyPassword;connect timeout=30;encrypt=True;trustservercertificate=False;application name=EntityFramework;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient"  />

(When i want to debug with remote datas, the local DB is obviously commented, and vice versa)

The weird thing is Identity is always going to the LOCAL DB (it even creates a new DB called MyRemoteDBName but locally the first time I launch my app).
However, Entity Framework is correctly going to the remote DB and display datas from remote DB !

I also have this class in my IdentityModels.cs :

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MyLocalDBName", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

I guess I'm wrong here : public ApplicationDbContext() : base("MyLocalDBName", throwIfV1Schema: false) ?

I tried to replace MyLocalDBName by MyRemoteDBName, but MyRemoteDBName is created locally (?!)

Why is Identity always going to the local DB whereas Entity Framework is correctly going to the remote DB ?

EDIT :

Here is how I instanciate my ApplicationDbContext :

ApplicationDbContext context = new ApplicationDbContext();

var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

Solution

  • The connection string you are using, is for EF ADO.NET Model. It can't be understand by Identity. So you need to add one more connection string for Identity, as:

    <!-- Local DB -->
    <add name="IdentityConnection" connectionString="data source=MY-PC\SQLEXPRESS;Initial Catalog=MyLocalDBName;integrated security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
    
    <!-- Remote DB -->
    <add name="IdentityConnection" 
         connectionString="data source=tcp:MyServerName.database.windows.net,1433;initial catalog=MyRemoteDBName;persist security info=False;user id=MyID;password=MyPassword;connect timeout=30;encrypt=True;trustservercertificate=False;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient"  />
    

    and use it on ApplicationDbContext as:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("IdentityConnection", throwIfV1Schema: false)
        {
        }
    
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }