Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure web service cannot connect to database

Tags:

azure

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.

enter image description here

like image 738
Alireza Avatar asked Oct 18 '22 10:10

Alireza


1 Answers

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
    }
}
like image 199
Ramazan Kürkan Avatar answered Nov 15 '22 06:11

Ramazan Kürkan