Reputation: 1969
I have been using code-first entity framework. I have defined a connection in the web.config, with name matching the class name of my DbContext (in this example 'MyDbContext').
EF uses this connection with no problems when the database exists.
However, if I delete the database (to force EF to re-create it) EF creates the database on the local Sql Express server instead.
If I specify the full connection string in the DbContext class constructor like
public class ReykerSCPContext : DbContext
{
public ReykerSCPContext() :
base("Server=tcp:MyDBSErver.database.windows.net,1433;Database=MyDB;User ID=#######;Password=#######;Trusted_Connection=False;Encrypt=True;MultipleActiveResultSets=True;PersistSecurityInfo=True;") { }
}
then the database is created correctly on the server of choice. Great!
Can anyone tell me if this is correct behaviour as I am bashing my brains out. I would like it all to work from the web.config, so I don't need to necessarily rebuild and remember to change settings in code etc..
Upvotes: 2
Views: 4247
Reputation: 4619
If you do
update-database -v
and it shows it is using IIS express instance instead of the value from the Web.Config , it means EF is confused when the local IIS express instance is running.
I had the same issue and was resolved by stop/delete IIS express.
"sqllocaldb.exe stop v11.0"
"sqllocaldb.exe delete v11.0”
Hop that helps someone!
Upvotes: 0
Reputation: 13131
This should do it:
public class EFDbContext : DbContext {
public EFDbContext() : base("EFDbContext") {
}
public DbSet<Product> Products { get; set; }
}
You should have the connection string by the name of your context class in the web.config and that will do it. The web.config should be in root.
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source = .; Initial Catalog = ITSDB; Integrated Security = true" providerName="System.Data.SqlClient"/>
</connectionStrings>
Upvotes: 4