I have one application using MVC3 w/ Razor that has been working perfectly working with one database. I am now encountering this error when trying to access the second database:
CREATE DATABASE permission denied in database ‘master’.
I assumed adding another DbContext class to the project would be the same process I used to add the first pre-created database, so I added the second connection string to the web.config just like this:
<connectionStrings>
<add name="DbContext1" connectionString="Data Source=db1.server.com;Initial Catalog=dbName1;User Id=db1;Password=*&#!$;" providerName="System.Data.SqlClient" />
<add name="DbContext2" connectionString="Data Source=db2.server.com;Initial Catalog=dbName2;User Id=db2;Password=*&#!$;" providerName="System.Data.SqlClient" />
I then added the second DbContext class to the application:
public class DbContext1 : DbContext
{
public DbSet<Table1> Object1 { get; set; }
public DbSet<Table2> Object2 { get; set; }
public DbSet<Table3> Object3 { get; set; }
public DbSet<Table4> Object4 { get; set; }
}
public class DbContext2 : DbContext
{
public DbSet<Table1> Object5 { get; set; }
}
The problem appears when I try to access the second database. The first database is still working like a charm, but for some reason, the second database appears to be trying to create a new database rather than read the existing database at the end of the connection string. Any help would be greatly appreciated!
Try explicitly selecting the context, either in the constructor of your classes:
Or, overloading the constructor to accept a connection string, and calling it in your code:
and then when you create a new context: