Deploying asp.net app with automatic migrations enabbled - asp.net

I have automatic migration enabled in my asp.net 4.6 mvc 5 web app. I added a custom field to IdentityUser model and when I run the web app I get
The model backing the 'ApplicationDbContext' context has changed since
the database was created. Consider using Code First Migrations to
update the database
I figured that I have to ran Update-Database from Package Manager Console and that resolved it locally in my dev environment. Now, when I deploy the app to an upstream environment (e.g. Azure) how do I trigger updating that database?

Try adding the below code to the Global.asax
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());

Related

.NET SQL Azure Migrations

I've finally managed to get my database to Azure! transformed my connection strings and it all logs in successfully after 3 weeks of trying.
Though just testing everything now and made a small change to my local model, ran a local migration and published again however when trying to access the Azure database it says I need to run a migration as the application DB context has changed.
How do I run migration on Azure?
Locally change the connection string to point to Azure Database (you will need to log into Azure Portal => SQL database => Select the database => Set Server Firewall => Add Client IP) and run update-database.
The correct approach would be enabling automatic database migrations.
That depends on how you publish the application, the version of Visual Studio and the development environment, eg. MVC 5 or Core. You can use the built in migration support from Visual Studio Publish, or set your default database to the Azure connection string and run Update-database from the Package Manager console again.
Look at the section on Database Migration here

Deployment Variables for Azure .Net Core Web App

I am having trouble getting the desired result from my CI/CD pipeline when deploying a .Net Core 2.0 web app to Azure.
As it stands everything is working when I deploy to my test environment. I have added a setting - ConnectionString:Main - to link to the correct database in the AppSettings section for the development app in the Azure portal.
I now want to deploy to my production environment. The issue is that there are two production databases, only one of which is "live" any any one time. What I would like to do is create two release definitions, one for each database and then have the ability to deploy using either one.
Is it possible to simply add a release variable that will override the local connection string in AppSettings.json as it was with previous .Net versions or is a more complex solution required?
You can use JSON variable substitution feature of Azure App Service Deploy task, for example, replace the value of ConnectionString in the sample below, you can define a release/environment variable as Data.DefaultConnection.ConnectionString in release definition.
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\SQLEXPRESS;Database=MyDB;Trusted_Connection=True"
}
}
}
When using ASP.NET Core apps on Azure, the recommended way to store secrets is by using Azure Key Vault. This makes sure that no credentials are stored in version control or in VSTS.
If you really want to update a configuration value during deployment, you can tokenize your parameters and replace them during deployment. You can use this marketplace task for that. Managing Config for .NET Core Web App Deployments with Tokenizer and ReplaceTokens Tasks describes how to use these tasks.
You should be able to accomplish this by creating the deployment via ARM template and creating different parameters files for each environment. Using the template the relevant app settings can be replaced, which will inject them into the Azure portal Application Settings. These settings will override what is checked into your config file in the repo.

Web App and Database Host on Azure

Hy Guys,
I'm new to Asp.net and Azure. I was working on ASP.NET with the help of this documentation link, When I publish my project on Azure by creating the Web App with latest portal on Azure, but it is only my application host on the Azure. And now i need to publish the my project database to Azure. In the present portal, the database option is not present there when we create the Web App on Azure to host the database as well with the Web project. But in the old portal as the documentation link guides us the database option is also present there when we publish our Web project on Azure.
Can anyone explain me how can i deploy my Web App existing database to Azure even if my Project has been deployed on Azure?
How can I deploy my existing Web App Database to Azure on the new portal.
If you use Code First Model of Entity Framework, you could just configure the connection string for your web application when you publish your web application. Entity Framework will generate tables for you automatically.
If there are existing data in your database which you want to migrate to Azure SQL Database. You could download a tool named Migration Assistant which will help you do it. For detail steps, link below is for your reference.
SQL Server database migration to SQL Database in the cloud
In the documentation he is using the old dashboard of Azure in which database option is attached with the Web App Publish procedure.
A small difference between old dashboard and new dashboard is that the creating Azure SQL Database feature is hidden when you create your app service. You need to click Explorer additional Azure services link to open the a new window for creating Azure SQL Database.
After creating a database, you could choose the database in the Setting tab when you publish your web application.
You also could create a database directly in Azure portal. After creating a database, you could get the connection string from the overview page.
If you make changes to a database schema and are using code first EF, will EF update your database schema when you redeploy your web app?
It depends whether you have enabled and run the migration. In the publish setting window, we could click the [Execute Code First Migrations] button to update the database schema .
If this button is disabled in the setting window, we need to run Enable-Migrations command in Package Manager Console.
In Visual Studio click menu Tool -> NuGet Package Manager -> Package Manager Console, input Enable-Migrations and run it.

Deploying ASP.NET Core solution with class library to Azure

I'm working in VS2015 and have a ASP.NET Core solution with two projects - an API Web Project and a Class Library that holds all the data entities, context and Entity Framework migrations. The API project references the class library and all works well on my local machine.
I now want to deploy the solution to Azure and this is where I'm hitting the problem. If I right click on the API project and go through the Azure App Service publish wizard, on the Settings tab I expand Databases and the message is "No databases found for this project" - which I'm guessing is because it can't find a context as it's not in this project.
If I do the same on the CL project though, there is no Azure App Service deployment option, the only option is File System and clearly there's no option to create the database there either.
So, in summary, my question is how I can deploy this type of solution to Azure and have the database created and migrations applied?
I think you need to create the DB first in the azure and then try to publish your application through the wizard. The database is on your local machine and the application will work just fine on your local environment. But on the cloud you have to first create the database on Azure SQL. Then you need to get the SQL connection string from the portal and update your config file accordingly. Once this is done you can then publish your application from Visual Studio. Please note that the wizard will still not show you the databases, but the application, when configured properly will run fine.

Running DNX (EF7) database migration on Azure

I've been running a MVC project with ASP.NET 4.6 and EF7 locally and everything works fine. I ran the dnx . ef migration Initial commands to create the database tables, and everything ran fine. The app works.
With Azure I have one problem, I can't seem to run the dnx . ef migration command so my SQL database is empty. I've debugged Startup.cs and the connection string is correctly retrieved, but the tables are not there.
I used the Publish option in Visual Studio 2015 to deploy to my Azure Web App.
How can I run this command on my web app? Is there another way to generate the database?
Thanks
It seems that the EF team has eliminated the infamous database initializers and offers a more versatile method for that on version 7.
Just use these methods in the pipeline before any call to the database,
yourDbContext.Database.EnsureCreated();
to create the database in case it doesn't exists and
yourDbContext.Database.Migrate();
to apply migrations (if you use this feature later). Both methods above also have an asynchronous version.
In my solution I have created an static class that I use to initialize the database and seed it with some data. I call the Seed method from the Configure method in the Startup class when some condition is met.

Resources