Reputation: 1271
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!
Upvotes: 0
Views: 143
Reputation: 20067
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.
Upvotes: 1