SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an...

70
Hands-On Lab SQL Azure Federations Lab version: 1.0.0 Last updated: 6/29/2022

Transcript of SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an...

Page 1: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Hands-On LabSQL Azure FederationsLab version: 1.0.0

Last updated: 5/8/2023

Page 2: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

CONTENTS

OVERVIEW................................................................................................................................................. 3

GETTING STARTED: CREATE YOUR SQL AZURE SERVER AND DATABASE....................................4Task 1 – Creating your SQL Azure Server....................................................................................4

Task 2- Creating a New Database.............................................................................................13

EXERCISE 1: WORKING WITH FEDERATIONS.....................................................................................15Task 1 – Federating an Existing Database.................................................................................15

Task 2 – Creating Federated Tables..........................................................................................18

Task 3 – Inserting Data.............................................................................................................21

Task 4 – Performing Federation Split Operations.....................................................................24

Verification...............................................................................................................................27

EXERCISE 2: WORKING WITH FEDERATED TABLES FROM ADO.NET.............................................33Task 1 – Adding Code to Query Federated Tables....................................................................33

Task 2 – Adding Code to Perform CRUD Operations on a Federated Table..............................39

Task 3 – Adding Code to Perform a Fan-Out............................................................................42

Verification...............................................................................................................................43

EXERCISE 3: WORKING WITH FEDERATED TABLES WITH ENTITY FRAMEWORK.........................48Task 1 – Adding Code to Query Federated Tables....................................................................48

Task 2 – Adding Code to Perform CRUD Operations on a Federated Table..............................55

Task 3 – Adding Code to Perform a Fan-Out............................................................................57

Verification...............................................................................................................................59

SUMMARY................................................................................................................................................ 64

Page 3: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Overview

One of the key value propositions of Windows Azure is the ability to achieve elastic scale by adapting dynamically to changes in demand. A single SQL Azure Database has limited capacity in terms of size and transactional throughput. To add more capacity you must partition your data across multiple SQL Azure databases. To release capacity, you must consolidate multiple SQL Azure databases into one. This approach is known as database sharding. SQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding implementations.

Objectives

In this Hands-On Lab, you will learn how to:

Add federations to a database and spit them into different federation members.

Query & Perform CRUD operations over federated tables using ADO.NET.

Query & Perform CRUD operations over federated tables using the ADO.NET Entity Framework.

Prerequisites

You must have the following items to complete this lab:

Microsoft Visual Studio 2010

Microsoft .NET Framework 4.0

ASP.NET and ASP.NET MVC 3

Windows Azure SDK and Windows Azure Tools for Microsoft Visual Studio 1.6

Using the Code Snippets

Throughout the lab document, you will be instructed to insert code blocks. For your convenience, most of that code is provided as Visual Studio Code Snippets, which you can use from within Visual Studio 2010 to avoid having to add it manually.

If you are not familiar with the Visual Studio Code Snippets, and want to learn how to use them, you can refer to the Setup.docx document in the Assets folder of the training kit, which contains a section describing how to use them.

Page 4: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Exercises

This Hands-On Lab contains the following exercises:

1. Exercise 1 – Working with Federations

2. Exercise 2 – Working with Federated Tables with ADO.NET

3. Exercise 3 – Working with Federated Tables with Entity Framework

Note: The source code that accompanies this hands-on lab includes in each folder an End folder where you can find a Visual Studio solution with the code as if you completed the steps in each exercise. You can use this solution as a guide if you need additional help working through the exercises.

If you’re running the End solutions without following the lab, notice that some may require manual configuration. To do this, please follow the instructions on each README.txt file located in the same folder of the End solution.

Estimated time to complete this lab: 60 minutes

Getting Started: Create your SQL Azure Server and Database

To follow this lab and complete all the exercises you first need to create a SQL Azure Server and a SQL Azure Database.

Task 1 – Creating your SQL Azure Server

In this task you will log into the SQL Azure portal to create your SQL Azure server.

1. Open Internet Explorer and go to the Windows Azure Management Portal at https://windows.azure.com

2. Log in using your Windows Live account.

Page 5: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 1Logging into the Azure Services Portal

3. At the Windows Azure Platform portal home page, click New Database Server on the ribbon.

Figure 2Creating a new SQL Azure database server

Page 6: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

4. Select your subscription on the left pane and click Create on the Server ribbon.

Figure 3Creating a new SQL Azure database server

5. Select a region from the Region drop down list, and then click Next. The location determines which datacenter the database will reside in.

Page 7: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 4Choosing region

6. Enter an administrator account name and password and click Next.

Page 8: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 5Entering administrator login and password

Note: An administrator account is a master account used to manage the new server. You should avoid using this account in connection strings where the username and password may be exposed.

7. Click Finish to create the new server. You will configure firewall rules later on this exercise.

Page 9: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 6Firewall rules dialog

8. Finally, the new server information, including Fully Qualified Server Name, is shown.

Figure 7SQL Azure projects list

Page 10: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Note: The fully qualified domain name of the server uses the following format:

<ServerName>.database.windows.net

where <ServerName> identifies the server, for example, a9ixtp7pux.database.windows.net.

9. Expand the subscription node located on the left pane and click on the server name you have created. The Server Information page allows you to perform basic administration of the database server.

Figure 8SQL Azure server information page

10. The Firewall Rules allows you to specify a list of IP addresses that can access your SQL Azure Server. The firewall will deny all connections by default, so be sure to configure your allow list so that existing clients can connect to the server.

Page 11: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 9Configuring the firewall settings for SQL Azure

Note: Changes to your firewall settings can take some time to become effective.

You now have a database server created and ready for the next steps in this lab. This database can be connected to from anywhere in the world.

11. Finally, the new server information, including Fully Qualified Server Name, is shown.

Page 12: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 10SQL Azure projects list

12. Expand the subscription node located on the left pane and click on the server name you have created. The Server Information page allows you to perform basic administration of the database server.

Figure 11SQL Azure server information page

13. The Firewall Rules allows you to specify a list of IP addresses that can access your SQL Azure Server. The firewall will deny all connections by default, so be sure to configure your allow list so that existing clients can connect to the server.

Page 13: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 12Configuring the firewall settings for SQL Azure

Note: Changes to your firewall settings can take some time to become effective.

You now have a database server created and ready for the next steps in this lab. This database can be connected to from anywhere in the world.

Task 2- Creating a New Database

In this task, you will create a new SQL Azure Database.

1. In the Windows Azure Management portal UI, select the Database option.

2. Under Subscriptions, expand your project in the tree view on the left, select the server name you created in the previous task and then click Create in the Database group of the ribbon.

Page 14: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 13Creating a new database

3. In the Create Database dialog, set the Database name to SalesDb, select the Web Edition and set the Maximum size to 1 GB.

Figure 14Choosing database features

Note: In this hands-on lab, you create a database using the SQL Azure portal. Databases can also be created by executing a DDL query against your assigned server using the T-SQL CREATE DATABASE statement, specifying which SQL Azure database edition (Web or Business) to create as well as its maximum size. For example, to create a Business Edition database with a maximum size of 30GB, use the following T-SQL command:

CREATE DATABASE HolTestDB (MAXSIZE = 30GB)

Page 15: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Once a database reaches its maximum size, you cannot insert additional data until you delete some data to free storage space or increase its maximum size.

4. Click OK and wait a few seconds for the process to finish. The portal will show you the new database.

Figure 15The Portal showing the new database

Exercise 1: Working with Federations

In this exercise you will add a federation to the recently created database. You will learn how to work with both federated tables and reference tables that are not federated. Additionally, you will learn how to split a Federation Member into two federation members.

Task 1 – Federating an Existing Database

Federation object can be created in any user database. When you first create your Federation Object, a Federation Member will be created, which is basically a system managed database, covering the full range of the data. Federations provide scale-out through federation members. In this task, you create a Federation over an UNIQUEIDENTIFIER field named CustId.

1. In the Windows Azure Management portal UI, select the Database option.

2. Under Subscriptions, expand your project in the tree view on the left, select the server name you created in Getting Started, and then click Manage in the Database group of the ribbon.

Page 16: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 16 Managing the Server

3. You will be redirected to the SQL Azure Management Portal. Enter your Server Administrator username and password and then click Log on.

Figure 17 Signing in to the SQL Azure Management Portal

Page 17: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

4. Once in the Database Administration page, click the New button in the upper ribbon to create a New Federation.

Figure 18 Creating a New Federation

5. In the Create a Federation page enter “Orders_Federation” as Name, “CustId” as Distribution Name and select “uniqueidentifier” as DataType. Then click the Save button.

6. Finally, information of the recently created Federation is shown.

Page 18: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 19 Information of the New Federation

Task 2 – Creating Federated Tables

Federation members can contain Federated Tables as well as regular objects that can exist in a SQL Azure database. These tables scale-out their data across all of the federation members. Tables created with the FEDERATED ON property are Federated Tables. In this task, you create the Products, Customers, Order and OrderItems tables, but only federate three of them.

1. In the Database Administration page, click the Orders_Federation link under Federations.

Figure 20 Editing the Federation

2. In the Federation Administration page, click the left column under LOW. A context menu will be displayed.

Page 19: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 21 Federation Member menu

3. Click Query and then click Create Federated Table.

Page 20: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 22 Choosing the Create Federated Table option

4. In the Query window, replace the proposed placeholder with the code below and then run the query to create the Products, Customers, Orders and OrderItems tables. Customers, Orders and OrderItems are being federated by the CustomerID field, while the Products table is not being federated, meaning that it will be cloned to each Federation Member. Please note the USE FEDERATION command at the top of the window. That means that the script will only run in the context of a Federation Member. Also, notice that the primary key column on each table needs to be of data type uniqueidentifier.

T-SQL

-- Create Products table (reference table, non-federated replicated)CREATE TABLE Products( ProductID int NOT NULL, SupplierID int NOT NULL, ProductName nvarchar(50) NOT NULL, Price decimal(12,2) NOT NULL, PRIMARY KEY(ProductId) )GO

-- Create Customers tableCREATE TABLE Customers(

Page 21: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

CustomerID uniqueidentifier NOT NULL, CompanyName nvarchar(50) NOT NULL, FirstName nvarchar(50), LastName nvarchar(50), PRIMARY KEY (CustomerId) )FEDERATED ON (CustId = CustomerID)GO

-- Create Orders tablecreate table Orders ( CustomerId uniqueidentifier NOT NULL, OrderId uniqueidentifier NOT NULL, OrderDate datetime, PRIMARY KEY (OrderId, CustomerId))FEDERATED ON (CustId = CustomerId)GO

-- Create OrderItems tableCREATE TABLE OrderItems( CustomerID uniqueidentifier NOT NULL, OrderID uniqueidentifier NOT NULL, ProductID int NOT NULL, Quantity int NOT NULL, PRIMARY KEY (OrderId, CustomerId, ProductId) )FEDERATED ON (CustId = CustomerId)GO

Task 3 – Inserting Data

In this task you will insert data to the recently created tables

1. Click the New Query button at the right of the upper ribbon and paste the following code

T-SQL

-- Connect to federationUSE FEDERATION Orders_Federation (CustId = '00000000-0000-0000-0000-000000000000') WITH RESET, FILTERING = OFFGO-- Insert sample dataINSERT INTO Products (ProductID, SupplierID, ProductName, Price)VALUES ( 386, 1001, 'Titanium Extension Bracket Left Hand', 5.25 )GOINSERT INTO Products (ProductID, SupplierID, ProductName, Price)VALUES ( 387, 1001, 'Titanium Extension Bracket Right Hand', 5.25 )GOINSERT INTO Products (ProductID, SupplierID, ProductName, Price)

Page 22: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

VALUES ( 388, 1001, 'Fusion Generator Module 5 kV', 10.50 )GOINSERT INTO Products (ProductID, SupplierID, ProductName, Price)VALUES ( 389, 1001, 'Bypass Filter 400 MHz Low Pass', 10.50 )GO

INSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'Van Nuys', 'Catherine', 'Abel')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'Abercrombie', 'Kim', 'Branch')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'Contoso', 'Frances', 'Adams')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'A. Datum Corporation', 'Mark', 'Harrington')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'Adventure Works', 'Keith', 'Harris')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'Alpine Ski House', 'Wilson', 'Pais')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'Baldwin Museum of Science', 'Roger', 'Harui')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'Blue Yonder Airlines', 'Pilar', 'Pinilla')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'City Power & Light', 'Kari', 'Hensien')GOINSERT INTO Customers (CustomerID, CompanyName, FirstName, LastName)VALUES (newid(), 'Coho Winery', 'Peter', 'Brehm')GO

DECLARE @orderId UNIQUEIDENTIFIERDECLARE @customerId UNIQUEIDENTIFIER

SET @orderId = NewId()SELECT @customerId = CustomerId FROM Customers WHERE LastName = 'Hensien' and FirstName = 'Kari'

INSERT INTO Orders (CustomerId, OrderId, OrderDate)VALUES (@customerId, @orderId, GetDate())

INSERT INTO OrderItems (CustomerID, OrderID, ProductID, Quantity)VALUES (@customerId, @orderId, 388, 4)

Page 23: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

SET @orderId = NewId()SELECT @customerId = CustomerId FROM Customers WHERE LastName = 'Harui' and FirstName = 'Roger'

INSERT INTO Orders (CustomerId, OrderId, OrderDate)VALUES (@customerId, @orderId, GetDate())

INSERT INTO OrderItems (CustomerID, OrderID, ProductID, Quantity)VALUES (@customerId, @orderId, 389, 2)

SET @orderId = NewId()SELECT @customerId = CustomerId FROM Customers WHERE LastName = 'Brehm' and FirstName = 'Peter'

INSERT INTO Orders (CustomerId, OrderId, OrderDate)VALUES (@customerId, @orderId, GetDate())

INSERT INTO OrderItems (CustomerID, OrderID, ProductID, Quantity)VALUES (@customerId, @orderId, 387, 3)

SET @orderId = NewId()SELECT @customerId = CustomerId FROM Customers WHERE LastName = 'Pais' and FirstName = 'Wilson'

INSERT INTO Orders (CustomerId, OrderId, OrderDate)VALUES (@customerId, @orderId, GetDate())

INSERT INTO OrderItems (CustomerID, OrderID, ProductID, Quantity)VALUES (@customerId, @orderId, 388, 1)GO

2. Click the Run button to run the script.

Page 24: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 23 Inserting Data

Task 4 – Performing Federation Split Operations

One of the key advantages of SQL Azure Federations is that allows the repartitioning of data through T-SQL commands like SPLIT, DROP or MERGE. By using a SPLIT command, rows in Federated Tables are moved from a Federated Member to a fresh, new Federated Member. In this task, you equally split the data in the Federated Tables through two federation members.

1. In the Database Administration page, click the New Query button.

2. In order to equally distribute data in two federated members, we need to find the CustomerId located at the half of the sample data. We will then use this CustomerId to perform a SPLIT operation and create two federation members. Run the following code and copy the result of the execution in a Notepad. You will use this result later on this task.

T-SQL

USE FEDERATION Orders_Federation (CustId = '00000000-0000-0000-0000-000000000000') WITH RESET, FILTERING = OFFGO

Page 25: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

CREATE TABLE #tmp ( Id int, CustomerId uniqueidentifier)

INSERT INTO #tmp (id, CustomerId)SELECT ROW_NUMBER() OVER(ORDER BY customerid), customerid FROM Customers ORDER BY CustomerId

DECLARE @count intSELECT @count = COUNT(*) FROM #tmp

SELECT CustomerId FROM #tmp WHERE Id = @count / 2

3. Click the [SalesDb].[Orders_Federation] in the left pane. Then click the left column under LOW.

Figure 24 Federation Member menu

Page 26: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

4. In the context menu select Split and enter the result of the query you run in step 3. Then click the Split button.

Figure 25 Split operation dialog

5. Click the Refresh button in the upper ribbon. The new Federation Member will be displayed.

Page 27: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 26 The new Federation Member

Verification

In order to verify the results of the tasks done so far, you will query the tables previously created and confirm that they were created and populated with data. Additionally, you will check how data is splitted in the two federation members. Finally, you will query the federation metadata to check the federated members’ data.

1. Click the New Query button in the upper ribbon.

2. Paste and run the following code. You will notice that, for example, the Customer result set only shows some of the customers we inserted in the table. This is because you are getting only data belonging to the first Federation Member. This is also true for Orders and OrderItems tables. You will also notice that for the Products table you are getting all the data. This is because the Products table is not federated, meaning that it is a reference table. Each Federation Member contains a copy of reference tables.

Note: You can switch between result sets windows by clicking the arrow next to the result count and selecting the desired result set.

Page 28: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

T-SQL

-- Connect to the first FederationUSE FEDERATION Orders_Federation (CustId = '00000000-0000-0000-0000-000000000000') WITH RESET, FILTERING = OFFGO

SELECT * FROM CustomersGO

SELECT * FROM OrdersGO

SELECT * FROM OrderItemsGO

SELECT * FROM ProductsGO

Figure 27Checking the values stored in a Federation Member

Page 29: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

3. Similarly, paste and run the following code. Please note that you will need to replace the highlighted placeholder with the CustomerId obtained in Task 3 step 3. In this case, you will be connecting to the second Federation Member created in the previous task. You will notice that the rest of the customers are now showed, as well as the rest of the orders and order items. This is because now you are retrieving data from another Federation Member. Also notice that once again all products are showed.

T-SQL

-- Connect to the second Federation-- NOTE: replace the placeholder with the CustomerId obtained in Task 3 step 3USE FEDERATION Orders_Federation (CustId = '{Replace with the CustomerId Obtained in Task 3 step 3}') WITH RESET, FILTERING = OFFGO

SELECT * FROM CustomersGO

SELECT * FROM OrdersGO

SELECT * FROM OrderItemsGO

SELECT * FROM ProductsGO

Page 30: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 28Checking the values of a specific Federated Key

4. In this step you will connect again to the second Federation Member but this time you will set the Filtering parameter to ‘ON’. By doing this, you are indicating that you want to connect the Federation Member where the specified CustomerId is located at and also that you want to retrieve only data belonging to that specific CustomerId. Paste and run the following code. Please note that you will need to replace the highlighted placeholder with the CustomerId obtained in Task 3 step 3. You will notice that only data for the specific CustomerId is showed.

T-SQL

-- Connect to the second Federation with Filtering = ON-- NOTE: replace the placeholder with the CustomerId obtained in Task 3 step 3USE FEDERATION Orders_Federation (CustId = '{Replace with the CustomerId Obtained in Task 3 step 3}') WITH RESET, FILTERING = ONGO

Page 31: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

SELECT * FROM CustomersGO

SELECT * FROM OrdersGO

SELECT * FROM OrderItemsGO

SELECT * FROM ProductsGO

Figure 29Checking the values of a specific Federated Key using Filtering=ON

5. Finally, you will query Federation Metadata. Paste and Run the following code. You will get metadata information regarding the Federation (stored in the Sys.Federations system view) and all the federation members (stored in the Sys.federation_member_distributions system view).

T-SQL

-- Query Federation Metadata USE FEDERATION ROOT WITH RESET

Page 32: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

GO -- FederationSELECT * FROM sys.FederationsGO

-- Federation MembersSELECT * FROM sys.federation_member_distributionsGO

Figure 30Querying the Medatada

6. Close the Explorer window.

Page 33: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Exercise 2: Working with Federated Tables from ADO.NET

In this exercise you will learn how develop applications that interact with federated tables using ADO.NET.

Task 1 – Adding Code to Query Federated Tables

In this task you will add logic to the application to query both Federated & reference tables.

1. Open Visual Studio as administrator from Start | All Programs | Microsoft Visual Studio 2010 by right clicking the Microsoft Visual Studio 2010 shortcut and choosing Run as administrator.

2. In the File menu, choose Open and then Project/Solution.

3. In the Open Project dialog, browse to Source\Ex02-WorkingWithFederatedTablesFromADO.NET\Begin, select FederationsWithEF.sln in the folder and click Open.

Page 34: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 31Begin solution

4. Make sure FederationsWithAdoNet is the startup project by right-clicking it in Solution Explorer and selecting Set as StartUp Project.

Figure 32Setting the startup project

5. Update the service definition to define the configuration settings required to access your SQL Azure Server. To do this, expand the Roles folder of the FederationsWithAdoNet project in Solution Explorer, right-click FederationsWithAdoNet.Web, and then select Properties.

Page 35: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

.Figure 33Launching the service configuration editor

6. Select the Settings tab, find the DataConnectionString setting and update the placeholders in the Value field with the settings of the server created in the Setup section of this Lab.

Figure 34Launching the service configuration editor

7. Open the FederationDb.cs file located in the Models folder.

8. Add the highlighted code to set the Federation Name and Distribution Name of your Federation.

(Code Snippet – SQL Azure Federation – Ex2 AddingFederationAndDistributionName– CS)

C#

public class FederationDb{ private const string FederationName = "Orders_Federation"; private const string DistributionName = "CustId";

private readonly string _connectionString;

Page 36: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

...}

9. Now, add the following method at the end of the class. This method will return the proper USE STATEMENT needed point queries to the correct Federation Member.

(Code Snippet – SQL Azure Federation – Ex2 GetUseFederationStatement– CS)

C#

private string GetUseFederationStatement(string federationName, string distributionName, Guid customerId, bool filteringOn){ return distributionName.ToUpper().CompareTo("ROOT") == 0 ? "USE FEDERATION ROOT WITH RESET" : string.Format("USE FEDERATION {0} ({1}='{2}') WITH RESET, FILTERING = {3}", federationName, distributionName, customerId, (filteringOn ? "ON" : "OFF"));}

10. Modify the GetDataSet method to execute a custom USE FEDERATION command before each query. Replace the entire method with the following code.

(Code Snippet – SQL Azure Federation – Ex2 GetDataSet– CS)

C#

private DataSet GetDataSet(string federationName, string distributionName, Guid customerId, bool filteringOn, string strSql, params SqlParameter[] parameters) { DataSet dataToReturn = null;

using (var temp = new DataSet()) { var useFederationCommandText = this.GetUseFederationStatement(federationName, distributionName, customerId, filteringOn);

using (var connection = new SqlConnection(this.connectionString)) { connection.Open();

using (var command = connection.CreateCommand()) { command.CommandText = useFederationCommandText; command.ExecuteNonQuery();

Page 37: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

}

using (var dataAdapter = new SqlDataAdapter()) { using (var command = new SqlCommand()) { foreach (var param in parameters) { command.Parameters.Add(param); }

command.Connection = connection; command.CommandText = strSql; dataAdapter.SelectCommand = command; dataAdapter.Fill(temp); } } }

dataToReturn = temp; }

return dataToReturn; }

11. In order to retrieve a single Order from the Database with federations, first you need to locate the Federation Member that contains the order you need. For this, you have to include the USE FEDERATION statement before the SELECT query with a specific customerId and its FILTERING attribute set to ON, to retrieve all orders of that particular customer. Notice that is not even necessary to include the customerId in the WHERE statement. This is the only difference between querying a standard database and a database with federations. In the GetOrderById method, replace the following line with the highlighted code.

(Code Snippet – SQL Azure Federation – Ex2 GetOrderById– CS)

C#

public Order GetOrderById(Guid customerId, Guid orderId){ var order = new Order(); var orders = this.GetDataSet(FederationName, DistributionName, customerId, true, "select * from Orders O join OrderItems OI on O.OrderId = OI.OrderId where O.OrderId = @orderId", new SqlParameter("@orderId", orderId));

if (orders.Tables.Count > 0)

Page 38: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

{ ...}

12. Now replace the highlighted code in the GetCustomerById method. This time, no WHERE clause is necessary, because this job is made in the USE FEDERATION statement. You only need to provide the customerId and set the FILTERING attribute to ON.

(Code Snippet – SQL Azure Federation – Ex2 GetCustomerById– CS)

C#

public Customer GetCustomerById(Guid customerId){ var customer = new Customer(); var customers = this.GetDataSet(FederationName, DistributionName, customerId, true, "select * from Customers");

if (customers.Tables.Count > 0) { ...}

13. Finally, we need to retrieve the Products from the Database with federations. For this, replace the highlighted line in the GetProductById method. Notice that we are always requesting data from the first Federation Member. This is because the Products table is not federated. Reference tables are replicated over all Federation members and updated every time a change is made. Therefore, is not necessary to target a specific Federation Member to retrieve a specific Product.

(Code Snippet – SQL Azure Federation – Ex2 GetProductById– CS)

C#

public Product GetProductById(int productId){ var product = new Product(); var products = this.GetDataSet(FederationName, DistributionName, Guid.Empty, false, "select * from products where ProductId = @productId", new SqlParameter("@productId", productId);

if (products.Tables.Count > 0) { ...}

14. In the same way, replace the highlighted code inside the GetAllProducts method.

Page 39: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

(Code Snippet – SQL Azure Federation – Ex2 GetAllProducts– CS)

C#

public List<Product> GetAllProducts(){ // non federated table var listProducts = new List<Product>(); var products = this.GetDataSet(FederationName, DistributionName, Guid.Empty, false, "select * from products");

if (products.Tables.Count > 0) { ...}

15. Press CTRL+S to save the changes.

Task 2 – Adding Code to Perform CRUD Operations on a Federated Table

Similarly to what happens when performing ‘Select’ operations, the only change needed to perform CRUD operations against a federated Database is the addition of the USE FEDERATION STATEMENT in order to point your query to the correct Federation Member. In this task you apply the necessary changes in the application to Add, Update and Delete an Order from a Database with federations.

1. If not already open, open the FederationDb.cs file located inside the Models folder.

2. You need to target the Federation member in which we are going to add the Order. Add the USE FEDERATION statement, and set a customerId within the Federation Member range. For this, locate the InsertOrder method and add the following code at the beginning of the method.

(Code Snippet – SQL Azure Federation – Ex2 InsertOrder– CS)

C#

private void InsertOrder(Order order){ var useFederationSatatement = this.GetUseFederationStatement(FederationName, DistributionName, order.CustomerId, true); var insertOrderStatement = "INSERT INTO ORDERS (CustomerId, OrderId, OrderDate) VALUES (@customerId, @orderId, @orderDate)"; ...}

Page 40: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

3. Add the following code to execute the USE FEDERATION STATEMENT after establishing the connection to the server.

(Code Snippet – SQL Azure Federation – Ex2 InsertOrderUseFederatedConnection– CS)

C#

private void InsertOrder(Order order){ ... using (var connection = new SqlConnection(this.connectionString)) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = useFederationSatatement; command.ExecuteNonQuery(); } ...}

4. Similarly, you have to target the Federation member where the Order to be updated is located. Add the following code at the beginning of the UpdateOrder method.

(Code Snippet – SQL Azure Federation – Ex2 UpdateOrder– CS)

C#

private void UpdateOrder(Order order){ var useFederationSatatement = this.GetUseFederationStatement(FederationName, DistributionName, order.CustomerId, true); var updateOrderStatement = "UPDATE ORDERS SET OrderDate = @orderDate WHERE OrderId = @orderId"; ...}

5. Add the following code to execute the USE FEDERATION STATEMENT after establishing the connection.

(Code Snippet – SQL Azure Federation – Ex2 UpdateOrderConnection– CS)

C#

private void UpdateOrder(Order order){ ...

Page 41: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

using (var connection = new SqlConnection(this.connection { connection.Open();

using (var command = connection.CreateCommand()) { command.CommandText = useFederationSatatement; command.ExecuteNonQuery(); } ...}

6. Finally, add the following code in the DeleteOrder method.

(Code Snippet – SQL Azure Federation – Ex2 DeleteOrder– CS)

C#

public void DeleteOrder(Guid customerId, Guid orderId){ var useFederationSatatement = this.GetUseFederationStatement(FederationName, DistributionName, customerId, true); ...}

7. Add the following code to execute the USE FEDERATION STATEMENT after establishing the connection.

(Code Snippet – SQL Azure Federation – Ex2 DeleteOrderConnection– CS)

C#

private void DeleteOrder(Order order){ ... using (var connection = new SqlConnection(this.connectionString)) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = useFederationSatatement; command.ExecuteNonQuery(); } ...

Page 42: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

}

8. Press CTRL+S to save the changes.

Task 3 – Adding Code to Perform a Fan-Out

Federation members are system managed databases that provide computational capacity to a part of federation’s data. An Application that uses a database with federation must be aware of this, and know that retrieving all items of a federated table could lead to a Fan Out query, requesting data from multiple databases. In this task you will add the logic to return all data from a database with federations.

1. If not already open, open the FederationDb.cs file located inside the Models folder.

2. Paste the following code inside the FanOut method. This method will be called inside the GetAllCustomers and the GetAllOrders methods.

(Code Snippet – SQL Azure Federation – Ex2 FanOut– CS)

C#

private DataSet FanOut(string sqlCommand){ Guid? federatedKey = Guid.Empty; DataSet dataToReturn = null; var sqlCommandText = sqlCommand;

using (var tempData = new DataSet()) { do { using (var data = new DataSet()) { using (var connection = new SqlConnection(this.connectionString)) { connection.Open();

// Connection Routing to the specified Federated Member using (var command = connection.CreateCommand()) { command.CommandText = this.GetUseFederationStatement(FederationName, DistributionName, (Guid)federatedKey, false); command.ExecuteNonQuery();

// Gets DataSet

Page 43: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

using (var dataAdapter = new SqlDataAdapter(sqlCommandText, connection)) { dataAdapter.Fill(data); }

// Merges data in temp dataset tempData.Merge(data);

// Get next range high value from metadata command.CommandText = "SELECT CAST(range_high as uniqueidentifier) FROM sys.federation_member_distributions"; var nextRange = command.ExecuteScalar();

if (nextRange == DBNull.Value) { federatedKey = null; } else { federatedKey = Guid.Parse(nextRange.ToString()); } } } } } while (federatedKey != null);

dataToReturn = tempData; }

return dataToReturn;}

3. Press CTRL+S to save the changes.

Verification

You will now launch the application in the compute emulator to verify the functionality with the changes applied to target a database with federations.

1. From Visual Studio, press F5 to launch the application in the Local Emulator.

2. The browser will show the default page of the application. In this page, you can see listed the orders of all the Customers.

Page 44: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 35SQL Azure Federations HOL HomePage

3. Click Create a new Order. You will be redirected to the Create Order page.

4. Click Add new Item and add two new items to the order, one for 5 Fusion Generator Module 5 kV and other for 7 Titanium Extension Bracket Right Hand.

Page 45: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 36Adding Order Items to the new Order

5. Press Create to save the new Order. You should see the newly created Order in the Orders List.

Page 46: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 37Showing the Orders List

6. Now, locate the Order you created in the previous step and click Edit. You will be redirected to the Edit Order page. Remove the last Order Item and click Save.

Page 47: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 38Editing an Order

7. You should see the order in the Orders List again, but this time with only 1 item. Now, click on Delete to remove your order from the Database.

Page 48: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 39Removing an Order

8. Close the Browser.

Exercise 3: Working with Federated Tables with Entity Framework

In this exercise you will learn how to develop applications that interact with federated tables using the ADO.NET Entity Framework.

Task 1 – Adding Code to Query Federated Tables

Similarly to what you have done in the previous exercise, before executing each query, you need to execute a USE FEDERATION statement to target a particular Federation member. In this task

Page 49: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

you will add logic to the application to query different tables from a database with federations using Entity Framework.

1. Open Visual Studio as administrator from Start | All Programs | Microsoft Visual Studio 2010 by right clicking the Microsoft Visual Studio 2010 shortcut and choosing Run as administrator.

2. In the File menu, choose Open and then Project/Solution.

3. In the Open Project dialog, browse to Source\Ex03-WorkingWithFederatedTablesWithEntityFramework\Begin, select FederationsWithEF.sln in the folder and click Open.

Figure 40Begin solution

4. Make sure FederationsWithEF is the startup project by right-clicking it in Solution Explorer and selecting Set as StartUp Project.

Page 50: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 41Setting the startup project

5. Update the service definition to define the configuration settings required to access your SQL Azure Server. To do this, expand the Roles folder of the FederationsWithEF project in Solution Explorer, right-click FederationsWithEF.Web, and then select Properties.

Figure 42Launching the service configuration editor

Page 51: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

6. Select the Settings tab, find the DataConnectionString setting and update the placeholders in the Value field with the settings of the server created in the Setup section of this Lab.

Figure 43Launching the service configuration editor

7. Open the FederationDb.cs file located inside the Models folder.

8. Add the highlighted code to set the Federation Name and Distribution Name of your database with federations.

(Code Snippet – SQL Azure Federation – Ex3 FederationDbConstants – CS)

C#

public class FederationDb{ private const string FederationName = "Orders_Federation"; private const string DistributionName = "CustId";

private readonly string connectionString;

...}

9. Now, add the following method at the end of the class to return a customized USE FEDERATION statement that allows you to query any database with federations.

(Code Snippet – SQL Azure Federation – Ex3 FederationDbFedStatement – CS)

C#

public class FederationDb{

...

private string GetUseFederationStatement(string federationName, string distributionName, Guid customerId, bool filteringOn)

Page 52: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

{ return distributionName.ToUpper().CompareTo("ROOT") == 0 ? "USE FEDERATION ROOT WITH RESET" : string.Format("USE FEDERATION {0} ({1}='{2}') WITH RESET, FILTERING = {3}", federationName, distributionName, customerId, (filteringOn ? "ON" : "OFF")); }}

10. Locate the GetOrderById method and add the highlighted line of code to set the USE FEDERATION command that is going to be executed before the query.

(Code Snippet – SQL Azure Federation – Ex3 GetOrderByIdUseFedCmd– CS)

C#

public Order GetOrderById(Guid customerId, Guid orderId){ var useFederationCommand = GetUseFederationStatement(FederationName, DistributionName, customerId, true); var order = new Order(); using (var context = new SalesEntities(this.connectionString)) { ... }

11. Now, add the code to execute the SqlCommand.

(Code Snippet – SQL Azure Federation – Ex3 GetOrderByIdConnOpen– CS)

C#

public Order GetOrderById(Guid customerId, Guid orderId){ ... using (var context = new SalesEntities(this.connectionString)) { ((IObjectContextAdapter)context).ObjectContext.Connection.Open(); context.Database.ExecuteSqlCommand(useFederationCommand); order = context.Orders.Single(o => o.OrderId == orderId); } return order;

Page 53: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

}

12. Repeat the same steps in the GetCustomerById method. First, add the highlighted line of code at the beginning of the method.

(Code Snippet – SQL Azure Federation – Ex3 GetCustomerByIdUseFedCmd– CS)

C#

public Customer GetCustomerById(Guid customerId){ var useFederationCommand = GetUseFederationStatement(FederationName, DistributionName, customerId, true);

var customer = new Customer(); ...}

13. Add the following code inside the using block.

(Code Snippet – SQL Azure Federation – Ex3 GetCustomerByIdConnOpen– CS)

C#

public Customer GetCustomerById(Guid customerId){ var useFederationCommand = GetUseFederationStatement(FederationName, DistributionName, customerId, true); var customer = new Customer(); using (var context = new SalesEntities(this.connectionString)) { ((IObjectContextAdapter)context).ObjectContext.Connection.Open(); context.Database.ExecuteSqlCommand(useFederationCommand); customer = context.Customers.Single(o => o.CustomerId == customerId); } return customer;}

14. Finally, we need to retrieve the Products from the database with federations. To do this, add the highlighted line in the GetProductById method. Notice that is not necessary to target a specific Federation member to retrieve the Products because the Products table is not federated.

(Code Snippet – SQL Azure Federation – Ex3 GetProductByIdUseFedCmd– CS)

Page 54: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

C#

public Product GetProductById(int productId){ var useFederationCommand = GetUseFederationStatement(FederationName, DistributionName, Guid.Empty, true); var product = new Product(); ...

15. Add the following line inside the using block, before the query to retrieve a single Product.

(Code Snippet – SQL Azure Federation – Ex3 GetProductByIdConnOpen– CS)

C#

public Product GetProductById(int productId){ ... using (var context = new SalesEntities(this.connectionString)) { ((IObjectContextAdapter)context).ObjectContext.Connection.Open(); context.Database.ExecuteSqlCommand(useFederationCommand); product = context.Products.Single(p => p.ProductId == productId); } return product;}

16. In the same way, add the highlighted code at the beginning of the GetAllProducts method.

(Code Snippet – SQL Azure Federation – Ex3 GetAllProductsUseFedCmd– CS)

C#

public List<Product> GetAllProducts(){ var useFederationCommand = GetUseFederationStatement(FederationName, DistributionName, Guid.Empty, true); var list = new List<Product>(); ...}

17. Add the highlighted code inside the using block.

Page 55: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

(Code Snippet – SQL Azure Federation – Ex3 GetAllProductsConnOpen– CS)

C#

public List<Product> GetAllProducts(){ ... using (var context = new SalesEntities(this.connectionString)) { ((IObjectContextAdapter)context).ObjectContext.Connection.Open(); context.Database.ExecuteSqlCommand(useFederationCommand); list = context.Products.ToList(); } return list;}

18. Press CTRL+S to save the changes.

Task 2 – Adding Code to Perform CRUD Operations on a Federated Table

As well as querying data, the only difference from performing a CRUD operation to a simple database and a database with federations is the addition of the USE FEDERATION statement. In this task you will apply the necessary changes in the application to Add, Update and Delete an Order from a database with federations using Entity Framework.

1. If not already open, open the FederationDb.cs file located inside the Models folder.

2. Locate the SaveOrder method. This method is used either to add or update data. To perform an Insert or an Update operation, you need to execute a USE FEDERATION statement to target the Federation member which you are going to work with. For this, locate the SaveOrder method and add the following line of code at the beginning of the method.

(Code Snippet – SQL Azure Federation – Ex3 SaveOrderUseFedCmd– CS)

C#

public void SaveOrder(Order order){var useFederationCommand = GetUseFederationStatement(FederationName, DistributionName, order.CustomerId, true); ...}

Page 56: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

3. Add the following code inside the using block to execute the command.

(Code Snippet – SQL Azure Federation – Ex3 SaveOrderConnOpen– CS)

C#

public void SaveOrder(Order order){ ... using (var context = new SalesEntities(this.connectionString)) { ((IObjectContextAdapter)context).ObjectContext.Connection.Open(); context.Database.ExecuteSqlCommand(useFederationCommand); ... }}

4. Similarly, you need to do the same for the DeleteOrder method. Add the highlighted code at the beginning of the method.

(Code Snippet – SQL Azure Federation – Ex3 DeleteOrderUseFedCmd– CS)

C#

public void DeletedOrder(Order order){ var useFederationCommand = GetUseFederationStatement(FederationName, DistributionName, order.CustomerId, true);

...}

5. Now, add the highlighted code inside the using block statement.

(Code Snippet – SQL Azure Federation – Ex3 DeleteOrderConnOpen– CS)

C#

public void DeletedOrder(Order order){ ... using (var context = new SalesEntities(this.connectionString)) { ((IObjectContextAdapter)context).ObjectContext.Connection.Open(); context.Database.ExecuteSqlCommand(useFederationCommand); context.Orders.Attach(order);

Page 57: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

context.Orders.Remove(order); context.SaveChanges(); }}

6. Press CTRL+S to save the changes.

Task 3 – Adding Code to Perform a Fan-Out

Federation members are system managed databases that provide computational capacity to a part of federation’s data. Applications that use a Database with federation must be aware of this, and know that retrieving all items from a Federated Table could lead to a Fan Out query, requesting data from multiple databases. In this task you will add the logic to return all data from Federated Tables.

1. If not already open, open the FederationDb.cs file located inside the Models folder.

2. To get all the Orders stored in the Database, we need to iterate through all federation members and retrieve the Orders located inside each one of them. For this, replace the code inside the GetAllOrders method with the following code. Take special consideration in how we are getting the Federation Key to target each Federation Member. This is made by requesting the maxRange of the current Federation Member from the sys.federation_member_distributions system view, because this is the value stored in the first row of the next Federation Member.

(Code Snippet – SQL Azure Federation – Ex3 GetAllOrders– CS)

C#

public List<Order> GetAllOrders(){ var list = new List<Order>(); Guid? federationKey = Guid.Empty; do { var useFederationStatement = GetUseFederationStatement(FederationName, DistributionName, (Guid)federationKey, false); using (var context = new SalesEntities(this.connectionString)) { ((IObjectContextAdapter)context).ObjectContext.Connection.Open(); context.Database.ExecuteSqlCommand(useFederationStatement);

Page 58: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

var auxList = context.Orders.Include("OrderItems").ToList(); list.AddRange(auxList); var maxRange = context.Database.SqlQuery<Guid?>("SELECT CAST(range_high as uniqueidentifier) FROM sys.federation_member_distributions").FirstOrDefault(); federationKey = (maxRange == null) ? null : maxRange; } } while (federationKey != null); return list;}

3. Similarly, paste the following code for the GetAllCustomers method.

(Code Snippet – SQL Azure Federation – Ex3 GetAllCustomers– CS)

C#

public List<Customer> GetAllCustomers(){ var list = new List<Customer>(); Guid? federationKey = Guid.Empty; do { var useFederationStatement = GetUseFederationStatement(FederationName, DistributionName, (Guid)federationKey, false); using (var context = new SalesEntities(this.connectionString)) { ((IObjectContextAdapter)context).ObjectContext.Connection.Open(); context.Database.ExecuteSqlCommand(useFederationStatement); var auxList = context.Customers.ToList(); list.AddRange(auxList); var maxRange = context.Database.SqlQuery<Guid?>("SELECT CAST(range_high as uniqueidentifier) FROM sys.federation_member_distributions").FirstOrDefault(); federationKey = (maxRange == null) ? null : maxRange; } } while (federationKey != null);

Page 59: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

return list;}

4. Press CTRL+S to save the changes.

Verification

You will now launch the application in the compute emulator to verify the functionality with the changes applied to target a database with federations.

1. From Visual Studio, press F5 to launch the application in the Local Emulator.

2. The browser will show the default page of the application. In this page, you can see the orders for all customers.

Figure 44SQL Azure Federations HOL HomePage

3. Click Create a new Order. You will be redirected to the Create Order page.

Page 60: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

4. Click Add new Item and add two new Order Items to the Order, one for 5 Fusion Generator Module 5 kV and other for a 7 Titanium Extension Bracket Right Hand.

Figure 45Adding Order Items to the new Order

5. Press Create to save the new Order. You should see the newly created Order in the Orders List.

Page 61: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 46Showing the Orders List

6. Now, locate the Order you created in the previous step and click Edit. You will be redirected to the Edit Order page. Remove the last Order Item and click Save.

Page 62: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 47Editing an Order

7. You should see the Order in the Orders List again, but this time with only 1 item. Now, click on Delete to remove your order from the Database.

Page 63: SQL Azure Federations - WOU Homepage - Western … · Web viewSQL Azure Federations is an integrated capability of SQL Azure Database that dramatically simplifies database sharding

Figure 48Removing an Order

8. Close the Browser.

Summary

By completing this hands-on lab you have learned how to implement database sharding using SQL Azure Federations, and how to work with federated tables from both ADO.NET and Entity Framework.