Complete guide on how to use Migrations with several Azure SQL databases, connectionstrings and Azure App Services publish profiles

asp.net-mvc azure-sql-database azure-web-app-service ef-migrations entity-framework-6

Question

I have a MVC project hosted in Azure App Services with Azure SQL databases for the data. I have a development, staging/acceptance and production environment (App Service) and as for the databases i have development, staging/acceptance, production and a local DB (mdf). I have 3 publish profiles (1 for each environment).

I (think) to understand that i can use the CTOR of the DbContext class to set a connectionstring from the web.config based on the name:

Ex.

public ApplicationDbContext(): base("DbContextNameThatResidesInWebConfig") 
{           
     // ...               
}   

If i am not using migrations i am able to do what i want without any issues. When using migrations (since i don't want any data loss when changing my model) i am getting several issues on model creation, i tried using several methods in my above CTOR:

// this is a custom Seeder that inherits DropDatabaseAlways/WhenModelChanges
System.Data.Entity.Database.SetInitializer(new ContractCareSeeder()); 

// from what i understand, this tells to use the latest migrations applied
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Migrations.Configuration>());

Configuration.cs:

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    // seeding method ...
}    

But i always receive errors like, "there is already a table called XXX ..." even when i have succesfully executed the Update-Database command in the console it still fails.

I am quite lost in this and can't seem to find what is the best way of handling my scenario. I want to be able to use multiple publish profiles (later on we will use development slots but not now) and there there is also configuration possible. I can see all my connections from my web.config along with update database checkbox but i can't seem to find the right way of configuring everything together...

Can anyone assist me, suggest me or provide any help of any kind? Kind regards!

1
0
11/22/2018 1:48:24 PM

Accepted Answer

ASP.NET 5 introduces improved support for controlling application behavior across multiple environments, such as development, staging, and production. Environment variables are used to indicate which environment the application is running in, allowing the app to be configured appropriately.

You could set ASPNET_ENV with the current environment you want to use. If you need to check whether the application is running in a particular environment, use env.IsEnvironment("environmentname")

if (env.IsDevelopment())
{
    app.UseBrowserLink();
    app.UseDeveloperExceptionPage();
    app.UseDatabaseErrorPage();
}
else
{
    app.UseExceptionHandler("/Home/Error");
}

For more details you could refer to this article and this one which though is asp.net core.

1
11/26/2018 9:53:44 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow