Azure With Azure SQL Database
Azure With Azure SQL Database
Azure With Azure SQL Database
NET app to
Azure with Azure SQL Database
Azure App Service provides a highly scalable, self-patching web hosting service. This
tutorial shows you how to deploy a data-driven ASP.NET app in App Service and
connect it to Azure SQL Database. When you're finished, you have an ASP.NET app
running in Azure and connected to SQL Database.
The app uses a database context to connect with the database. In this sample, the
database context uses a connection string named MyDbConnection. The connection string
is set in the Web.config file and referenced in the Models/MyDatabaseContext.cs file. The
connection string name is used later in the tutorial to connect the Azure app to an Azure
SQL Database.
You can keep the generated web app name, or change it to another unique name (valid
characters are a-z, 0-9, and -). The web app name is used as part of the default URL for
your app (<app_name>.azurewebsites.net, where <app_name> is your web app name). The
web app name needs to be unique across all apps in Azure.
Note
Don't select Create yet.
A resource group is a logical container into which Azure resources, such as web apps,
databases, and storage accounts, are deployed and managed. For example, you can
choose to delete the entire resource group in one simple step later.
An App Service plan specifies the location, size, and features of the web server farm that
hosts your app. You can save money when you host multiple apps by configuring the
web apps to share a single App Service plan.
TABLE 1
3.
Before creating a database, you need a logical SQL server. A logical SQL server is a
logical construct that contains a group of databases managed as a group.
The server name is used as part of the default URL for your
server, <server_name>.database.windows.net. It must be unique across all
servers in Azure SQL. Change the server name to a value you want.
Remember this username and password. You need them to manage the
server later.
Important
Even though your password in the connection strings is masked (in Visual
Studio and also in App Service), the fact that it's maintained somewhere
adds to the attack surface of your app. App Service can use managed
service identities to eliminate this risk by removing the need to maintain
secrets in your code or app configuration at all. For more information,
see Next steps.
6. Click OK.
Note
If you see Local user secrets files instead, you must have configured SQL
Database from the Connected Services page instead of the Publish page.
Visual Studio lets you explore and manage your new database in Azure easily in the SQL
Server Object Explorer. The new database already opened its firewall to the App
Service app that you created, but to access it from your local computer (such as from
Visual Studio), you must open a firewall for your local machine's public IP address. If
your internet service provider changes your public IP address, you need to reconfigure
the firewall to access the Azure database again.
2. Select the database that you created earlier. The connection you created
earlier is automatically filled at the bottom.
Once Visual Studio finishes creating the firewall setting for your SQL
Database instance, your connection shows up in SQL Server Object
Explorer.
Here, you can perform the most common database operations, such as run
queries, create views and stored procedures, and more.
You can use the familiar tools in Visual Studio to update your database and app in
Azure. In this step, you use Code First Migrations in Entity Framework to make a change
to your database schema and publish it to Azure.
For more information about using Entity Framework Code First Migrations, see Getting
Started with Entity Framework 6 Code First using MVC 5.
C#Copy
public bool Done { get; set; }
Run Code First Migrations locally
PowerShellCopy
Enable-Migrations
3. Add a migration:
PowerShellCopy
Add-Migration AddProperty
5. Type Ctrl+F5 to run the app. Test the edit, details, and create links.
If the application loads without errors, then Code First Migrations has succeeded.
However, your page still looks the same because your application logic is not using this
new property yet.
Make some changes in your code to use the Done property. For simplicity in this tutorial,
you're only going to change the Index and Create views to see the property in action.
1. Open Controllers\TodosController.cs.
C#Copy
public ActionResult Create([Bind(Include =
"Description,CreatedDate,Done")] Todo todo)
3. Open Views\Todos\Create.cshtml.
C#Copy
<div class="form-group">
@Html.LabelFor(model => model.Done, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Done)
@Html.ValidationMessageFor(model => model.Done, "", new
{ @class = "text-danger" })
</div>
</div>
</div>
5. Open Views\Todos\Index.cshtml.
6. Search for the empty <th></th> element. Just above this element, add the
following Razor code:
C#Copy
<th>
@Html.DisplayNameFor(model => model.Done)
</th>
C#Copy
<td>
@Html.DisplayFor(modelItem => item.Done)
</td>
You can now add a to-do item and check Done. Then it should show up in your
homepage as a completed item. Remember that the Edit view doesn't show
the Done field, because you didn't change the Edit view.
Now that your code change works, including database migration, you publish it to your
Azure app and update your SQL Database with Code First Migrations too.
Now that you enabled Code First Migrations in your Azure app, publish your code
changes.
2. Try adding to-do items again and select Done, and they should show up in
your homepage as a completed item.
All your existing to-do items are still displayed. When you republish your ASP.NET
application, existing data in your SQL Database is not lost. Also, Code First Migrations
only changes the data schema and leaves your existing data intact.
You can stream tracing messages directly from your Azure app to Visual Studio.
Open Controllers\TodosController.cs.
Each action starts with a Trace.WriteLine() method. This code is added to show you how
to add trace messages to your Azure app.
2. In Cloud Explorer, expand the Azure subscription that has your app and
expand App Service.
However, you don't see any of the trace messages yet. That's because when
you first select View Streaming Logs, your Azure app sets the trace level
to Error, which only logs error events (with the Trace.TraceError() method).
3. In the portal management page for your app, from the left menu,
select App Service logs.