Skip to content

Latest commit

 

History

History

Handling-change-EF-migrations

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Handling change (EF migrations, Deployment rollback)


Overview

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.

Goals

In this demo, you will see how to:

  1. Update the object model and database accordingly using Entity Framework migrations
  2. Deploy to Microsoft Azure using Git
  3. Rollback to a previous deployment using the Azure Portal

Key Technologies

  1. Entity Framework 7.
  2. Git.

Setup and Configuration

Follow these steps to setup your environment for the demo.

  1. 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.

  2. Configure an Azure SQL Database following the steps provided in this link. Copy the ADO.NET connection string value.

  3. 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.

  4. 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

    Default connection

  5. Configure the GeekQuiz web site to support Publishing with Git and push the duplicate of the begin solution to the remote repository.

  6. Leave the Azure Portal in a separate browser window/tab.

  7. Navigate to the created site and register an account.

  8. Enable the ASP.NET Core command-line tools. Open a command-prompt and run:

    dnvm upgrade
    
  9. Open Visual Studio 2015.

  10. Open the GeekQuiz.sln solution located under source\begin.

  11. Run the solution and register a new user in order to generate the SQL database.

  12. In Visual Studio, close all open files.

  13. Make sure that you have an Internet connection, as this demo requires it to push to a remote git repository.

  14. Open the SQL Server Object Explorer and dock it in the left panel.

  15. Open the Solution Explorer and dock it in the right panel.

Demo

This demo is composed of the following segments:

  1. Migrations
  2. Deployment rollback

Migrations

  1. 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.

  2. 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

    Creating the initial migration

  3. Back in Visual Studio, show that the new migration was created inside the TriviaDb folder located under the Migrations folder.

    Showing the initial migration

    Showing the initial migration

  4. In the Command Prompt, enter the following command and then press Enter.

    dnx ef database update --context TriviaDbContext --verbose

    Applying the initial migration

    Applying the initial migration

  5. 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

    Trivia Questions Columns

  6. In Solution Explorer, double-click the TriviaQuestion.cs file located inside the Models folder.

  7. 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; }
    }
  8. 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 the Down method reverts those changes.

  9. In the Command Prompt, enter the following command and then press Enter.

    dnx ef database update --context TriviaDbContext --verbose
  10. In SQL Server Object Explorer, click Refresh.

    Clicking the refresh button

    Clicking the refresh button

  11. 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

    Showing the new Hint Column

Deployment Rollback

  1. Open the GeekQuiz.sln solution that you copied to a separate folder during the setup phase.

  2. Double-click the AnswersService.cs file in Solution Explorer.

  3. 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;
    }
  4. Press CTRL + S to save the changes.

  5. 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
  6. Open the web site using Microsoft Edge.

  7. Log-in using the previously created credentials.

    Logging in

    Logging in

  8. Press F12 to open the development tools.

  9. Select the network tab, make sure that it is recording and clear the session.

    Starting Network Recording

    Starting Network Recording

  10. Select any of the four answers. Nothing will happen.

  11. Show that the web request failed with a 500 error.

    Showing the 500 error

    Showing the 500 error

    Speaking point: This clearly point to the last change. Let's rollback to the previous working version.

  12. Do not close the GeekQuiz site, and switch to the browser window/tab that has the Azure Portal open.

  13. 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

    Showing the existing deployments

  14. Select the initial commit and click REDEPLOY.

    Redeploying the initial commit

    Redeploying the initial commit

  15. When prompted to confirm, click YES.

  16. Once the deployment is finished, switch back to the web site and press CTRL + F5.

  17. Click any of the options. The flip animation will take place and the result (correct/incorrect) will be displayed.


Summary

By completing this demo you should have:

  1. Used Entity Framework migrations to update GeekQuiz database to reflect the changes in the object model
  2. Deployed a change (bug) to Microsoft Azure using Git
  3. Rollback to the last working deployment using the Azure Portal