I have created a web service and a SQL database on Azure and I have deployed my Asp.net web app and upload my database on Azure SQL server. After that I created an connection string to connect my web app to database, but my web app cannot connect to database.
Well, I had this problem, too. The cause of my problem is my publish settings.
Summary of my problem,
I used Visual Studio(VS) to publish my app service to azure with default publish settings, and even though I could access front-end contents, I couldn't access back-end services. For database operations, I used Entity Framework and migration.
For local machines, everything was fine, but on azure not.
Answer,
I simple uncheck "Execute Code First Migrations" checkbox in "Settings" step in publish modal window, and on azure everything was fine, but I faced another problem, no migration at all. To solve the migration problem, I added a small code in Global.asax.cs
:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// For migration
try
{
System.Data.Entity.Database.SetInitializer(
new System.Data.Entity.MigrateDatabaseToLatestVersion<MyWebApp.Contexts.ApplicationDbContext, MyWebApp.Migrations.Configuration>());
var context = MyWebApp.Contexts.ApplicationDbContext.Create();
context.Roles.Find("JustForDBMigrationKick");
context.Dispose();
}
catch (System.Exception e)
{
// Ooops, something went wrong
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With