SQL Azure With Entity Framework

Introduction

In this article, we will see the following things -

  • Prerequisites
  • Implementation – Creating simple Empty MVC Web application
  • Implementation – Connecting current application with Azure SQL using Entity Framework.

Prerequisites

  • Some basic knowledge of Azure, like how to create in SQL Azure.
  • Read this article for the basics of SQL Azure.
Implementation – Creating a simple empty MVC Web application
  • Open Visual Studio that is preinstalled in your system.
  • Go to File - New - Project and select Web Template - ASP.NET Web Application.

    Azure
  • Then, select empty MVC Template, as shown below.

    Azure
  • Now, create Home Controller in the Controllers folder.
  • Right click on Controllers folder and click on Add - Controller.

    Azure 
  • Add an empty Controller.

    Azure
  • Now, create an empty View.
  • For that, right click on the index method and Add View.

    Azure
  • Click on "Add" button.

Implementation – Connecting current application with Azure SQL using Entity Framework.

  • Now, create a SQL database in Azure.
  • To create a SQL database in Azure, follow this article.

  • Azure
  • Connect it with local SQL Server using server name as the connection string and create a Employee table in it, as shown below.

    Azure
  • Now, configure the database in your application using Entity Framework.
  • Right click on Project and install Entity Framework from NuGet packages by choosing the option “Manage packages”.

    Azure 
  • Now, add the .edmx file as DB first approach and connect with SQL Azure entities, as shown below.

    Azure 
  • Select EF designer from Database, as shown below.

    Azure 
  • Then, choose the server name from SQL Azure DB Properties on the portal and create SQL Server connection using the credentials; choose DB from there and click on OK.

    Azure 
  • Now, choose the table from the menu and click "Finish".

    Azure 
  • Now, go to Controller and add some lines of code to insert into Employee table.

    Azure 
  • Now, run the application and check employee table data from the SQL Server Object Explorer.

    Azure 

This is how we can use SQL Azure with Entity Framework.

Happy Learning.

Up Next
    Ebook Download
    View all
    Learn
    View all