0

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!

Dimitri
  • 1,185
  • 2
  • 15
  • 37
  • Any process now? – Joey Cai Nov 29 '18 at 05:23
  • @JoeyCai, no not yet, i have to continue develop certain features, once this is done i will try the migrations scenario again and see what my findings are, i will post back here. Thank you for following up. – Dimitri Nov 29 '18 at 07:54

1 Answers1

1

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.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Joey Cai, determing the environments or databases are not the issue, and i am not using Core so i can't set the Environment variables as far as i know. The issue i have is that i am confused about how to correctly use the Migrations along with seeders for multiple environments (App Services publish profiles) AND Azure databases. I don't want to loose data when changing my model. I can use it properly without Migrations and publish to the right database or App Service but when adding migrations i get issues about existing tables for example. (if i need to clear up some things let me know) – Dimitri Nov 26 '18 at 15:13
  • 1
    Without migrations, how do you switch your environment? – Joey Cai Nov 27 '18 at 02:25
  • 1
    You could refer to this article [Entity Framework Data Migrations for Different Environments](https://stackoverflow.com/questions/41934490/entity-framework-data-migrations-for-different-environments). – Joey Cai Nov 27 '18 at 02:42
  • Joey Cai, I switch environments by setting a contextName in my ApplicationDbContext CTOR (which determines my database), when publishing i have a publish profile for each environment. There is the issue in my opinion, in my publish profile(s) => Configure => Settings I can't make it work without receiving an error, i have tried different ways by checking/unchecking the "Update database" option or "Execute Code First Migrations". When i have migrations enabled i can publish but i can't write to my database configured in this configuration. – Dimitri Nov 27 '18 at 07:44
  • Thank you for the Link Joey Cai i will check that out today, looks a good way of using different environment but i don't think this will solve my issue with migrations. Will post an update. (Thank you) – Dimitri Nov 27 '18 at 07:45