Click here to Skip to main content
15,861,172 members
Articles / Hosted Services / Azure
Tip/Trick

ASP.NET 5 - Connect to SQL Azure Database

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
3 Jan 2016CPOL3 min read 14K   8   1
Using MVC, Entity Framework, ASP.NET5 Scaffolding, and Azure SQLServer you can create a web application that stores your information on an SQL Azure database. This demo shows you how to create a web application with MVC and Entity Framework 7, that communicate with a SQL Azure Database

Introduction

Using MVC, Entity Framework, ASP.NET5 Scaffolding, and Azure SQLServer you can create a web application that stores your information on an SQL Azure database. This demo shows you how to create a web application with MVC and Entity Framework 7, that communicate with a SQL Azure database.

STEP 1 - Create Azure Account

You need to get a Windows Azure account. Everyone can open a Windows Azure account for free.

Check the link below for more information.

http://www.windowsazure.com/en-us/pricing/free-trial/

 

STEP 2 - Create SQL Database on Windows Azure

After get access to an Azure Account, we need to create a SQL Database to store your data.

So for that we need to select the option New on the left bottom of our web page and then select the option Data + Storage -> SQL Database-> Set the name and provide the configurations you need.

On this case our SQL Database will have the name "SQLDemoAzure".

After created the SQL Database, we need to get the connection string that will be used on Web Aplication to access the Azure SQL Database.

For that, select the databse created and on the main window, on the right side, we have an option called "Show Connection String".

When we select that option, a new tabwill appear, like the following image, with the connection string formatted to different providers.

 

STEP 3 - Create ASP.NET 5 Web Application

  • Open Visual Studio 2015 and create a new project of type ASP.NET 5 Web Application.
  • On this project I create a solution called Demo.

 

  • Press OK, and a new screen will appear, with several options of template to use on our project.
  • Select the option MVC.

 

After selection of our template, your first web application using ASP.NET 5 is created.

 

 

STEP 4 - Create Data Model

After we have our web application created, we need to ceate our data model.

For that, select the option Add New Item on solution and choose the option Class. Create the class like the one on the image above. 

 

 

STEP 5 - Scaffolding

 

This could be made easily using the Scaffolding functionality.

On the solution on the top of controller folder, select the option Add New Scaffold Item.

On the new screen, select the option MVC6 Controller with views using entity framework.

 

Select the name of the controller, class model and data context class.

 

The new controllers and views associated, was created with sucess.

 

 

STEP 6 - Change Connection String

For that just copy your database connection string, as explain on step1 and past into appsettings.json file.

 

STEP 7 - Change Menu Layout

To test the two tables, we can add two new entries on the web application menu.

 

STEP 8 - Run Application

Press now the F5 button, to run the web application.

The new entities appear on the menu.

 

Press the option car to see our entiity in action

Checking the database, you can see the table corresponding to the entity created and the data inserted.

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Devscope
Portugal Portugal
I am João Sousa, and since i finish my degree I’m working in software development using Microsoft technologies.

I was awarded

Microsoft Most Valuable Professional (MVP) 2015 – .Net

My profissional profile:

Azure Developer
.NET Developer

My Certifications:

MCTS - .NET Framework - Application Development Foundation
MCTS - .NET Framework 2.0 - Windows-based Client Development
MCTS - .NET Framework 3.5 ADO.NET Applications
MCTS - .NET Framework 3.5 ASP.NET Applications
MCSD - Programming in HTML5 with JavaScript and CSS3
MCSD - Developing ASP.NET MVC 4 Web Applications
MCSD - Developing Windows Azure and Web Services
MCSA Office 365 - Managing Office 365 Identities and Requirements
MCSA Office 365 - Enabling Office 365 Services
MCSD - Implementing Microsoft Azure Infrastructure Solutions

Comments and Discussions

 
GeneralNice explanation Pin
MukeshKumar893-Jan-16 19:08
MukeshKumar893-Jan-16 19:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.