.NET
Raiting:
5

ASP.NET CORE - Connecting to Azure SQL


Asp.net MVC developers working with MNC’s have in-depth experience in developing MVC application. In this post, they will explain how to create a sample app with asp.net 5, which will store the data in Azure SQL. They are using Entity Framework and Scaffolding of asp.net MVC in the sample. For more information, read blog further.

In this post we will create a sample application using ASP.NET 5. That application will store the data in the Azure SQL using Entity Framework and Scaffolding of asp.net MVC to support the basic operations (CRUD).

First of all you will need to have access to the Microsoft Azure. For that purpose, you can create a free account. You can get more information about how to achieve that in that link: https://azure.microsoft.com/en-us/free/
Creating a DB in MS Azure

After creating an account (or you may already have one) we have to create a new instance of SQL which will provide space to host our information.

For that you should select (+) in the lateral menu in the left side, and then, on the options that appear in the first column, you should choose an option Data and Storage.

After that select the option in the second column that appears, where we are provided with several data providers to choose from.

Let's select SQL Database, given that we will use that provider in our article.

image

Then, is necessary to configure selected database. In our case let’s give it a name of "sqlAzureDemo" (a name of database).

After creating a database, we need to generate a connection to that database, which will be used in our core 1.0 application. In order to achieve that, if we select recently created database, the way is shown on the image, there is an option "Show Connection String", which will provide us an access to what we need. Let's save that connection string for future references.
Creating a Web App

In order to start creating our application, lets select an option ASP.NET web application. (I'm using the visual studio built in templates for that purpose).

image

First let’s give a name to our application (you can choose the name that is most convenient for you) and then click OK. Now we should be redirected to the new screen where we can choose a template to be used within our application. Since we are developing an application in ASP.NET 5, we have several option to choose between. In our case we must choose the last option that you see on the screen (Web Application).

image

After selecting a template, the application will be generated and configured automatically by visual studio.
Creating a Model for Data

After visual studio is finished to create an application the next step would be to define a model of data to be used. For that in our web project, let's create a new class.

Let's suppose that we are working with vehicles product, so we will define some simple properties to represent our vehicles:

public class Car
{
[Key]
public int Identification {get;set;}
public string Model {get;set;}
public int Weight {get;set;}
}
Adding Scaffolding
Scaffolding will save us a lot of time, providing us with possibility to generate code automatically. We will use scaffolding for our initial CRUD operations. Based on a single class and without writing a single line of code we will be able to generate a controller for our CRUD operations and associated views for each controller.

In the solution select the controller’s directory, and select (clicking with the right button of your mouse) Add New Scaffold Item. The new screen should appear where you should choose “MVC6 Controller With Views Using Entity Framework”.

image

Now you should have your controllers and associated views created automatically.
Adding a Connection String
In order to change the default connection string, copy the connection string you saved previously from Azure and then put it in you appsettings.json file.
Changing Menu in The Layout
In order to be able to test our entity, we will add new item in menu select list, which is located in the layout file. The behavior that we want to achieve is when we click on that menu item we will be provided with data from entity created.
Run an Application
Now you may press the f5 key (or select debug -> start debugging) and the application will be launched in the browser. The new entities created will appear on the screen in the top menu, the same way as you can see on the following image:

image

If you select new Vehicles, we will be able to access the screen that was generated automatically by scaffolding.

If anything is unclear, ask ASP.Net MVC developers straight away. They will respond to your comments on the page itself.
ethanmillar 31 august 2016, 11:11
Vote for this post
Bring it to the Main Page
 

Comments

Leave a Reply

B
I
U
S
Help
Avaible tags
  • <b>...</b>highlighting important text on the page in bold
  • <i>..</i>highlighting important text on the page in italic
  • <u>...</u>allocated with tag <u> text shownas underlined
  • <s>...</s>allocated with tag <s> text shown as strikethrough
  • <sup>...</sup>, <sub>...</sub>text in the tag <sup> appears as a superscript, <sub> - subscript
  • <blockquote>...</blockquote>For  highlight citation, use the tag <blockquote>
  • <code lang="lang">...</code>highlighting the program code (supported by bash, cpp, cs, css, xml, html, java, javascript, lisp, lua, php, perl, python, ruby, sql, scala, text)
  • <a href="http://...">...</a>link, specify the desired Internet address in the href attribute
  • <img src="http://..." alt="text" />specify the full path of image in the src attribute