Skip to content

Commit 973d4c4

Browse files
committed
changes
1 parent b286b62 commit 973d4c4

File tree

4 files changed

+74
-30
lines changed

4 files changed

+74
-30
lines changed

articles/app-service/app-service-web-tutorial-dotnet-sqldatabase.md

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how to deploy a data-driven C# ASP.NET app to Azure App Servi
44
ms.assetid: 03c584f1-a93c-4e3d-ac1b-c82b50c75d3e
55
ms.devlang: csharp
66
ms.topic: tutorial
7-
ms.date: 05/28/2025
7+
ms.date: 06/05/2025
88
ms.custom: devx-track-csharp, mvc, devcenter, vs-azure, AppServiceConnectivity
99
author: cephalin
1010
ms.author: cephalin
@@ -31,7 +31,9 @@ In this tutorial, you:
3131
## Prerequisites
3232

3333
- [!INCLUDE [quickstarts-free-trial-note](~/reusable-content/ce-skilling/azure/includes/quickstarts-free-trial-note.md)]
34-
- Install <a href="https://www.visualstudio.com/downloads/" target="_blank">Visual Studio 2022</a> with the **ASP.NET and web development** and **Azure development** workloads. You can add the workloads to an existing Visual Studio installation by selecting **Get Tools and Features** in the Visual Studio **Tools** menu.
34+
- Install <a href="https://www.visualstudio.com/downloads/" target="_blank">Visual Studio 2022</a> with the **ASP.NET and web development** and **Azure development** workloads.
35+
- You can add the workloads to an existing Visual Studio installation by selecting **Get Tools and Features** in the Visual Studio **Tools** menu.
36+
- Make sure you have the latest version of Visual Studio by selecting **Help** > **Check for updates** and installing the latest version if necessary.
3537

3638
## Create and run the app
3739

@@ -56,8 +58,6 @@ The sample project contains a basic [ASP.NET MVC](https://www.asp.net/mvc) creat
5658

5759
To publish the app to Azure, you create and configure a Publish profile that has an Azure App Service and App Service Plan to host the app. You then create an Azure SQL Server and Azure SQL database to contain the app data, and configure a database context to connect the app with the database.
5860

59-
The database context in this sample is a connection string named `MyDbConnection`. The connection string is set in the *Web.config* file and referenced in the *Models/MyDatabaseContext.cs* file. The Azure app uses the connection string name to connect to the Azure SQL database.
60-
6161
1. In Visual Studio **Solution Explorer**, right-click the **DotNetAppSqlDb** project and select **Publish**.
6262

6363
![Screenshot that shows Publish from Solution Explorer.](./media/app-service-web-tutorial-dotnet-sqldatabase/solution-explorer-publish.png)
@@ -125,30 +125,23 @@ Before you can create a database, you need a [logical SQL server](/azure/azure-s
125125

126126
1. Change the server name to a value you want. The server name must be unique across all servers in Azure SQL.
127127

128-
1. Add an administrator username and password. For password requirements, see [Password policy](/sql/relational-databases/security/password-policy).
129-
130-
Remember this username and password. You need them to manage the server later.
128+
1. Select **OK**.
131129

132130
![Screenshot that shows creating the server.](media/app-service-web-tutorial-dotnet-sqldatabase/configure-sql-database-server.png)
133131

134-
> [!IMPORTANT]
135-
> Your password in the connection strings is masked in both Visual Studio and App Service, but maintaining it still adds to the attack surface of your app. App Service can use [managed service identities](overview-managed-identity.md) to eliminate this risk by removing the need to maintain secrets in your code or app configuration. For more information, see [Tutorial: Connect to SQL Database from App Service without secrets using a managed identity](tutorial-connect-msi-sql-database.md).
136-
137-
1. Select **OK**.
138-
139132
1. On the **Azure SQL Database** screen, keep the default generated **Database Name**. Select **Create** and wait for the database resources to be created.
140133

141134
![Screenshot that shows configuring the database.](media/app-service-web-tutorial-dotnet-sqldatabase/configure-sql-database.png)
142135

136+
1. When the database resources are created, select **Next**.
137+
143138
### Connect the database
144139

145140
The app uses a database context to connect with the database. The database context in this sample is a connection string named `MyDbConnection`. The connection string is set in the *Web.config* file and referenced in the *Models/MyDatabaseContext.cs* file. The Azure app uses the connection string name to connect to the Azure SQL database.
146141

147-
1. When the database resources are created, select **Next**.
148-
149142
![Screenshot of the screen with messagea about configuring managed identity for the connection to work.](./media/app-service-web-tutorial-dotnet-sqldatabase/connect-warning.png)
150143

151-
1. Under **Connection string name**, enter the name of the connection string referenced in *Models/MyDatabaseContext.cs*, in this case *MyDbConnection*.
144+
1. On the **Connect to Azure SQL Database** screen, under **Connection string name**, enter the name of the connection string referenced in *Models/MyDatabaseContext.cs*, in this case *MyDbConnection*.
152145

153146
> [!NOTE]
154147
> If you see **Local user secrets files** instead, make sure you used the **Publish** page, not the **Connected Services** page, to configure SQL Database.
@@ -157,11 +150,73 @@ The app uses a database context to connect with the database. The database conte
157150

158151
Your app is connected to Azure SQL Database using Managed Identity for Azure services, a secure method of connecting your app to your Azure resources that doesn't use secrets or passwords.
159152

160-
Note the message that you need to set the appropriate permissions on the SQL user corresponding with this managed identity for the connection to work. For more information, see [Grant permissions to managed identity](tutorial-connect-msi-sql-database.md#grant-permissions-to-managed-identity).
153+
You now need to set the appropriate permissions on the SQL user corresponding with this managed identity for the connection to work.
154+
155+
## Configure managed identity
156+
157+
When the Azure SQL Database creation wizard set up the Azure SQL server with a managed identity and Entra ID Default authentication, it added your Entra ID account as the Azure SQL admin. If you're signed in to the same account in Visual Studio, you can use the same `Authentication=Active Directory Default` connection string to connect to the database in both Visual Studio and Azure.
158+
159+
1. From the **Tools** menu, select **NuGet Package Manager** > **Package Manager Console**.
160+
161+
1. In the **Package Manager Console**, install the following packages:
162+
163+
```powershell
164+
Install-Package Microsoft.Data.SqlClient
165+
Install-Package Microsoft.EntityFramework.SqlServer
166+
```
167+
168+
1. In a PowerShell command line, run the following command to sign in to SQL Database, replacing `<server-name>` with your server name, `<db-name>` with your database name, and `<entra-id-user>` with your Microsoft Entra user name.
169+
170+
```azurepowershell
171+
sqlcmd -S <servername>.database.windows.net -d <dbname> -U <entra-id-user> -G -l 30
172+
```
173+
174+
1. At the SQL prompt, run the following commands to grant the minimum permissions your app needs, replacing `<app-name>` with your app name.
175+
176+
```sql
177+
CREATE USER [DotNetAppSqlDb20250604144735] FROM EXTERNAL PROVIDER;
178+
ALTER ROLE db_datareader ADD MEMBER [DotNetAppSqlDb20250604144735];
179+
ALTER ROLE db_datawriter ADD MEMBER [DotNetAppSqlDb20250604144735];
180+
ALTER ROLE db_ddladmin ADD MEMBER [DotNetAppSqlDb20250604144735];
181+
GO
182+
```
183+
184+
1. In *web.config*, remove the `entityFramework/providers/provider` section and line: `<provider invariantName="System.Data.SqlClient" .../>`
185+
186+
1. In *Models/MyDatabaseContext.cs*, add the following class:
187+
188+
```csharp
189+
public class AppServiceConfiguration : MicrosoftSqlDbConfiguration
190+
{
191+
public AppServiceConfiguration()
192+
{
193+
SetProviderFactory("System.Data.SqlClient", Microsoft.Data.SqlClient.SqlClientFactory.Instance);
194+
SetProviderServices("System.Data.SqlClient", MicrosoftSqlProviderServices.Instance);
195+
SetExecutionStrategy("System.Data.SqlClient", () => new MicrosoftSqlAzureExecutionStrategy());
196+
}
197+
}
198+
```
199+
200+
1. Add the following attribute to *MyDatabaseContext.cs*:
201+
202+
```csharp
203+
[DbConfigurationType(typeof(AppServiceConfiguration))]
204+
```
205+
206+
### Allow client connection from your computer
207+
208+
By default, the Azure server allows connections to its databases only from Azure services, such as your Azure app. The new database opened its firewall to the App Service app you created.
209+
210+
To access the database from your local computer, such as from Visual Studio, the Azure server must open the firewall to allow access for the machine's public IP address.
211+
212+
If prompted to add access for your local client IP address, make sure to select the option to **Allow your computer's public IP address**. This option creates a firewall rule to allow the public IP address of your local computer. The dialog box is already populated with your computer's current IP address.
213+
214+
>[!NOTE]
215+
>If your internet service provider changes your public IP address, you need to reconfigure the firewall to access the Azure database again.
161216
162217
### Deploy the ASP.NET app
163218

164-
1. When the connection succeeds, at the top of the **Publish** tab, select **Publish**. Your ASP.NET app deploys to Azure, and your default browser launches to the URL of the deployed app.
219+
1. At the top of the **Publish** tab, select **Publish**. Your ASP.NET app deploys to Azure, and your default browser launches to the URL of the deployed app.
165220

166221
1. To test the app, add a few to-do items.
167222

@@ -189,17 +244,6 @@ You can use Visual Studio **SQL Server Object Explorer** to easily explore and m
189244

190245
![Screenshot that shows exploring SQL Database objects.](./media/app-service-web-tutorial-dotnet-sqldatabase/explore-sql-database.png)
191246

192-
### Allow client connection from your computer
193-
194-
By default, the Azure server allows connections to its databases only from Azure services, such as your Azure app. The new database opened its firewall to the App Service app you created.
195-
196-
To access the database from your local computer, such as from Visual Studio, the Azure server must open the firewall to allow access for the machine's public IP address.
197-
198-
If prompted to add access for your local client IP address, make sure to select the option to **Allow your computer's public IP address**. This option creates a firewall rule to allow the public IP address of your local computer. The dialog box is already populated with your computer's current IP address.
199-
200-
>[!NOTE]
201-
>If your internet service provider changes your public IP address, you need to reconfigure the firewall to access the Azure database again.
202-
203247
## Update the app with Code First Migrations
204248

205249
You can use 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 change your database schema and publish the change to Azure.
@@ -291,15 +335,15 @@ Make some changes in your code to see the `Done` property in action. For this tu
291335
292336
### Enable Code First Migrations in Azure
293337
294-
Now that your code change works, including database migration, you can publish it to your Azure app and update your SQL Database with Code First Migrations too.
338+
Now that your code change works, including database migration, you can publish it to your Azure app and update your Azure SQL database with Code First Migrations too.
295339
296340
1. In Visual Studio **Solution Explorer**, right-click your project and select **Publish**.
297341
298342
1. Select **More actions** > **Edit** to open the publish settings.
299343
300344
![Screenshot that shows opening publish settings.](./media/app-service-web-tutorial-dotnet-sqldatabase/publish-settings.png)
301345
302-
1. In the **MyDatabaseContext** dropdown, select the database connection for your Azure SQL Database.
346+
1. In the **MyDatabaseContext** dropdown, select the database connection for your Azure SQL database.
303347
304348
1. Select **Execute Code First Migrations (runs on application start)**, and then select **Save**.
305349
-2.21 KB
Loading
-2.88 KB
Loading
4.42 KB
Loading

0 commit comments

Comments
 (0)