In this demo you go through the steps of enabling Entity Framework migrations to GeekQuiz database, changing the model and understanding how those changes are reflected in the database. Additionally, you will deploy to Azure using Git and perform a rollback to the previous deployment from Azure Portal.
In this demo, you will see how to:
- Update the object model and database accordingly using Entity Framework migrations
- Deploy to Microsoft Azure using Git
- Rollback to a previous deployment using the Azure Portal
Follow these steps to setup your environment for the demo.
-
Copy the contents of the source\begin folder to a separate directory. Both demo segments start from the same begin solution, so you will need to remember the directory to where you copied the files for the second segment.
-
Configure an Azure SQL Database following the steps provided in this link. Copy the ADO.NET connection string value.
-
Create a new Web App in Azure Portal.
Note: To avoid issues with the local storage while deploying with git, use a Basic Service Plan or greater.
-
In the Application settings of your new Web App, update the connection string key for the DB to DefaultConnection and value copied from previous step. Save the changes.
Default connection
-
Configure the GeekQuiz web site to support Publishing with Git and push the duplicate of the begin solution to the remote repository.
-
Leave the Azure Portal in a separate browser window/tab.
-
Navigate to the created site and register an account.
-
Enable the ASP.NET Core command-line tools. Open a command-prompt and run:
dnvm upgrade
-
Open Visual Studio 2015.
-
Open the GeekQuiz.sln solution located under source\begin.
-
Run the solution and register a new user in order to generate the SQL database.
-
In Visual Studio, close all open files.
-
Make sure that you have an Internet connection, as this demo requires it to push to a remote git repository.
-
Open the SQL Server Object Explorer and dock it in the left panel.
-
Open the Solution Explorer and dock it in the right panel.
This demo is composed of the following segments:
-
In the Solution Explorer, select the GeekQuiz project and press Shift + Alt + , to open a Command Prompt in the folder where the project is located.
-
In the Command Prompt you just opened, enter the following command and then press Enter. An initial migration based on the existing model will be created.
dnx ef migrations add InitialMigration --context TriviaDbContext
Creating the initial migration
-
Back in Visual Studio, show that the new migration was created inside the TriviaDb folder located under the Migrations folder.
Showing the initial migration
-
In the Command Prompt, enter the following command and then press Enter.
dnx ef database update --context TriviaDbContext --verbose
Applying the initial migration
-
In SQL Server Object Explorer, expand the different nodes until the columns of the dbo.TriviaQuestions table are displayed. This is shown in the following figure.
Trivia Questions Columns
-
In Solution Explorer, double-click the TriviaQuestion.cs file located inside the Models folder.
-
Add the Hint property, as shown in the following code snippet.
public class TriviaQuestion { public int Id { get; set; } [Required] public string Title { get; set; } public virtual List<TriviaOption> Options { get; set; } public string Hint { get; set; } }
-
Switch back to the Command Prompt, enter the following command and then press Enter. A new migration will be created.
dnx ef migrations add QuestionHint --context TriviaDbContext
Speaking point: Explain that the migration only accounts for the diff between the current model and the one from the previous migration. The
Up
method applies the changes to the target database and theDown
method reverts those changes. -
In the Command Prompt, enter the following command and then press Enter.
dnx ef database update --context TriviaDbContext --verbose
-
In SQL Server Object Explorer, click Refresh.
Clicking the refresh button
-
Expand the different nodes until the columns of the dbo.TriviaQuestions table are displayed. The new Hint column will be displayed.
Showing the new Hint Column
-
Open the GeekQuiz.sln solution that you copied to a separate folder during the setup phase.
-
Double-click the AnswersService.cs file in Solution Explorer.
-
Replace the StoreAsync method implementation with the following snippet.
public async Task<bool> StoreAsync(TriviaAnswer answer) { var selectedOption = await this.db.TriviaOptions.FirstOrDefaultAsync(o => MatchesOption(answer, o)); if (selectedOption != null) { answer.TriviaOption = selectedOption; this.db.TriviaAnswers.Add(answer); await this.db.SaveChangesAsync(); } return selectedOption.IsCorrect; } private static bool MatchesOption(TriviaAnswer answer, TriviaOption o) { var a = answer.OptionId / 0; return o.Id == answer.OptionId && o.QuestionId == answer.QuestionId; }
-
Press CTRL + S to save the changes.
-
Open the Git console and enter the following commands.
git add . git commit -m "Refactored answer check to a different method" git push azure master
-
Open the web site using Microsoft Edge.
-
Log-in using the previously created credentials.
Logging in
-
Press F12 to open the development tools.
-
Select the network tab, make sure that it is recording and clear the session.
Starting Network Recording
-
Select any of the four answers. Nothing will happen.
-
Show that the web request failed with a 500 error.
Showing the 500 error
Speaking point: This clearly point to the last change. Let's rollback to the previous working version.
-
Do not close the GeekQuiz site, and switch to the browser window/tab that has the Azure Portal open.
-
Open the Web app and select Continuous deployment under PUBLISHING in the Settings blade. Both commits will be listed in the deployment history.
Showing the existing deployments
-
Select the initial commit and click REDEPLOY.
Redeploying the initial commit
-
When prompted to confirm, click YES.
-
Once the deployment is finished, switch back to the web site and press CTRL + F5.
-
Click any of the options. The flip animation will take place and the result (correct/incorrect) will be displayed.
By completing this demo you should have:
- Used Entity Framework migrations to update GeekQuiz database to reflect the changes in the object model
- Deployed a change (bug) to Microsoft Azure using Git
- Rollback to the last working deployment using the Azure Portal