• No results found

Lab A: Storing Event Data in Azure SQL Databases

In document 20532A-ENU-TrainerHandbook.pdf (Page 123-127)

Scenario

Now that the web application is ready for publishing, you can begin migrating the web application to Azure by first creating a database in Azure. You have decided to use a database initializer and Entity Framework Code First to automate the creation of your database in SQL Azure Databases.

Objectives

After you complete this lab, you will be able to:

• Create an Azure SQL Databases server and database instance using the Management Portal.

• Use Entity Framework Code First to initialize and seed a database in the cloud.

• Use the SQL Azure Databases Management Portal to view live data in the cloud.

Estimated Time: 45 minutes

For this lab, you will use the available host machine. Before you begin this lab, you must complete the following steps:

Prior to starting this lab, you must have completed the labs in Modules 2 and 3.

1. On the host computer, click Start, type Remote, and then click Remote Desktop Connection.

2. In Remote Desktop Connection, provide the name of your virtual machine in the Computer dialog box using this format:

vm20532a[Your Name Here].cloudapp.net:[Your VM RDP Port]

Note: The name and port for your VM may already be saved in the drop-down list when selecting a Computer. If this is the case, use this value instead of typing it in manually. If you are unsure about your VM’s RDP port, use either of the Azure portal to look up your VM’s endpoints.

The endpoint with the name Remote Desktop is the correct port for RDP. This port is randomized to protect your VM from unauthorized access.

3. In Remote Desktop Connection, click Connect. Wait until the RDP client accesses the virtual machine.

4. If necessary, sign in using the following credentials:

o User name: testuser o Password: TestPa$$w0rd

5. Verify that you received credentials to log into the Azure portal from you training provider, these credentials and the Azure account will be used throughout the labs of this course.

Exercise 1: Creating an Azure SQL Databases Instance

Scenario

In this exercise you will:

• Create a SQL Server instance in Azure.

• Create a database in Azure.

The main tasks for this exercise are as follows:

1. Login to the Azure Preview Portal

MCT USE ONL Y. STUDENT USE PROHIBITED

7-14 Storing Tabular Data in Azure

2. Create an Azure SQL Database using the Preview Portal

Task 1: Login to the Azure Preview Portal

1. Log into the Azure Preview Portal (https://portal.azure.com).

Task 2: Create an Azure SQL Database using the Preview Portal

1. View the list of SQL Databases for your subscription.

2. Create a SQL Database with the following details:

o Name: db20532a[Your Name Here]

o Edition: Web

o Server Name: sv20532a[Your Name Here]

o Server Admin Login: testuser

o Server Admin Password: TestPa$$w0rd

o Location: Select the region that is closest to your location 3. Record the name of the newly created SQL Database instance.

Results: After completing this exercise, you will be able to create both servers and databases in the Azure SQL Databases service.

Exercise 2: Using Entity Framework with Azure SQL Databases

Scenario

In this exercise you will:

Configure an Entity Framework Code First DatabaseInitializer.

• Implement a Seed method for the initializer.

• Publish a Cloud Services application.

• View tables and data using the Azure SQL Databases Management Portal.

The main tasks for this exercise are as follows:

1. Run the ASP.NET Web Application to View Events from LocalDb 2. Configure the DbContext with a New DatabaseInitializer 3. Implement Seed Data with the DbContext

4. Publish the Cloud Application with the Updated DbContext to Azure 5. Verify that the Azure Cloud Service Website is using the New Data 6. Login to the Azure Management Portal

7. View the Migrated Data in the Azure SQL Management Portal

Task 1: Run the ASP.NET Web Application to View Events from LocalDb

1. Open the Contoso.Events solution from the following location:

o File location: Allfiles (E):\Mod07\Labfiles\Starter\SQL\Contoso.Events

MCT USE ONL Y. STUDENT USE PROHIBITED

Developing Microsoft Azure™ Solutions 7-15

2. Debug the Contoso.Events.DataGeneration console project.

Note: This Data Generation console application simply creates some sample data in your local SQL database (LocalDb) so that you can test the site.

3. Debug the Contoso.Events.Cloud cloud project.

Task 2: Configure the DbContext with a New DatabaseInitializer

1. Create a new EventsContextInitializer class in the Contoso.Events.Data project.

2. Update the EventsContextInitializer class with the public accessor.

3. Update the EventsContextInitializer class to inherit from the DropCreateDatabaseAlways<EventsContext> class.

4. Add a using statement for the Contoso.Events.Models namespace.

5. Update the EventsContext static constructor with the following lines of code:

Database.SetInitializer<EventsContext>(

new EventsContextInitializer() );

Task 3: Implement Seed Data with the DbContext

1. Override the Seed method of the DropCreateDatabaseAlways<> class.

2. Add a using statement for the Contoso.Events.Models namespace.

3. At the end of the Seed method, create an instance of the EventItem class with the following values:

o EventKey: "FY17SepGeneralConference"

o StartTime: DateTime.Today

o EndTime: DateTime.Today.AddDays(3d) o Title: "FY17 September Technical Conference"

o Description: "Sed in euismod mi."

o RegistrationCount: 1

4. At the end of the Seed method, add the newly created EventItem to the context.Events collection.

5. At the end of the Seed method, create an instanced of the Registration class with the following values:

o EventKey: "FY17SepGeneralConference"

o FirstName: "Aisha"

o LastName: "Witt"

6. At the end of the Seed method, add the newly created Registration to the context.Registrations collection.

7. At the end of the Seed method, invoke the context.SaveChanges() method.

8. Build the Contoso.Events.Data project.

MCT USE ONL Y. STUDENT USE PROHIBITED

7-16 Storing Tabular Data in Azure

Task 4: Publish the Cloud Application with the Updated DbContext to Azure

1. Update the Web.Release.config file in the Contoso.Events.Web project to use a connection string to your Azure SQL Database.

2. Publish the Contoso.Events.Cloud project to a new Cloud Service project with the following details:

o Name: cs20532a[Your Name Here]

o Region: Select the region that is closest to your location o Environment: Production

o Build configuration: Release 3. Service configuration: Cloud

Task 5: Verify that the Azure Cloud Service Website is using the New Data

1. View the website for your published Cloud Service.

Task 6: Login to the Azure Management Portal

1. Log into the Azure Management Portal (https://manage.windowsazure.com).

Task 7: View the Migrated Data in the Azure SQL Management Portal

1. View the list of SQL Databases for your subscription.

2. View the dashboard for your previously created SQL Database:

o Name: db20532a[Your Name]

3. Log in to the Management Portal for your SQL Database with these values:

o Username: testuser o Password: TestPa$$w0rd

4. View the data in the dbo.Events table.

Results: After completing this exercise, you will be able to configure Entity Framework to initialize a new database with seed data.

Question: What are some scenarios where it is appropriate to use Seed data from a ORM framework in your Azure SQL Database?

MCT USE ONL Y. STUDENT USE PROHIBITED

Developing Microsoft Azure™ Solutions 7-17

Lesson 6

In document 20532A-ENU-TrainerHandbook.pdf (Page 123-127)

Related documents