Q101. Connect to Windows Azure SQL Database Using the ADO.NET Entity Framework?

The ADO.NET Entity Framework enables developers to create data access applications by programming against a conceptual application model instead of programming directly against a relational storage schema. This topic shows how to create a simple conceptual model that is mapped to a Windows Azure SQL Database and query over the conceptual model.

Let us consider a school administration code example. In this example, we create strongly-typed query against the CLR objects that represents entities and associations. This query returns the department names in the school. To follow this example, you must install the Entity Framework from the .NET Framework 3.5 Service Pack 1 or later versions.

Create the data model

In Visual Studio, on the File menu, point to New, and then click Project.

In the Project types pane, select Windows. In the Templates pane, select Console Application. Enter SchoolApplicationEF as the Name, and then click OK.

In Solution Explorer, right-click SchoolApplicationEF, point to Add, and select New Item.

Select ADO.NET Entity Data Model, enter SchoolDataModel.edmx as the Name, and then click Add.

In the Entity Data Model Wizard, select Generate from database and then click Next.

Click New Connection, and enter the information for the School database on your SQL Database server, click OK. Then, click Next. Select Tables and then click Finish. This creates an .edmx file that defines an entity type for each table in the database. Save the SchoolDataModel.edmx file.

When running this code, the Entity Framework cannot create the .edmx file and related client classes from a SQL Database. It must connect to a local instance of SQL Server to generate the model and mapping files. However, the Entity Framework will connect to Windows Azure SQL Database when the model and mapping files have been created.

Create the Console Application

Replace the code in your program file with the following code, and then press F5 to run the application against your SQL Database.

connecting to azure sql database using ado.net entity framework