You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: azure-sql/database/azure-sql-dotnet-entity-framework-core-quickstart.md
+83-64Lines changed: 83 additions & 64 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,12 +23,12 @@ This quickstart describes how to connect an application to a database in Azure S
23
23
24
24
- An [Azure subscription](https://azure.microsoft.com/pricing/purchase-options/azure-account?icid=azurefreeaccountdotnet/).
25
25
- A SQL database configured for authentication with Microsoft Entra ID ([formerly Azure Active Directory](/entra/fundamentals/new-name)). You can create one using the [Create database quickstart](./single-database-create-quickstart.md).
26
-
-[.NET 7.0](https://dotnet.microsoft.com/download) or later.
26
+
-[.NET 9.0](https://dotnet.microsoft.com/download) or later.
27
27
-[Visual Studio](https://visualstudio.microsoft.com/vs/) or later with the **ASP.NET and web development** workload.
28
28
- The latest version of the [Azure CLI](/cli/azure/get-started-with-azure-cli).
29
29
- The latest version of the Entity Framework Core tools:
30
-
* Visual Studio users should install the [Package Manager Console tools for Entity Framework Core](/ef/core/cli/powershell).
31
-
* .NET CLI users should install the [.NET CLI tools for Entity Framework Core](/ef/core/cli/dotnet).
30
+
- Visual Studio users should install the [Package Manager Console tools for Entity Framework Core](/ef/core/cli/powershell).
31
+
- .NET CLI users should install the [.NET CLI tools for Entity Framework Core](/ef/core/cli/dotnet).
32
32
33
33
## Configure the database server
34
34
@@ -46,7 +46,7 @@ The steps in this section create a .NET Minimal Web API by using either the .NET
46
46
47
47
1. For the **Project Name**, enter *DotNetSQL*. Leave the default values for the rest of the fields and select **Next**.
48
48
49
-
1. For the **Framework**, select .NET 7.0 and uncheck **Use controllers (uncheck to use minimal APIs)**. This quickstart uses a Minimal API template to streamline endpoint creation and configuration.
49
+
1. For the **Framework**, select .NET 9.0 and uncheck **Use controllers**. This quickstart uses a Minimal API template to streamline endpoint creation and configuration.
50
50
51
51
1. Choose **Create**. The new project opens inside the Visual Studio environment.
52
52
@@ -76,15 +76,7 @@ To connect to Azure SQL Database by using .NET and Entity Framework Core, you ne
76
76
-**Microsoft.EntityFrameworkCore.SqlServer**: Provides extra components to connect to the logical server
77
77
-**Microsoft.EntityFrameworkCore.Design**: Provides support for running Entity Framework migrations
78
78
-**Microsoft.EntityFrameworkCore.Tools**: Provides support for Visual Studio Package Manager Console tooling (PowerShell only)
79
-
80
-
Alternatively, you can also run the `Install-Package` cmdlet in the **Package Manager Console** window:
@@ -106,12 +99,7 @@ The Entity Framework Core libraries rely on the `Microsoft.Data.SqlClient` and `
106
99
107
100
Complete the following steps to connect to Azure SQL Database using Entity Framework Core and the underlying `DefaultAzureCredential` class:
108
101
109
-
1. Add a `ConnectionStrings` section to the `appsettings.Development.json` file so that it matches the following code. Remember to update the `<your database-server-name>` and `<your-database-name>` placeholders.
110
-
111
-
The passwordless connection string includes a configuration value of `Authentication=Active Directory Default`, which enables Entity Framework Core to use `DefaultAzureCredential` to connect to Azure services. When the app runs locally, it authenticates with the user you're signed into Visual Studio with. Once the app deploys to Azure, the same code discovers and applies the managed identity that is associated with the hosted app, which you'll configure later.
112
-
113
-
> [!NOTE]
114
-
> Passwordless connection strings are safe to commit to source control, since they do not contain any secrets such as usernames, passwords, or access keys.
102
+
1. Add a `ConnectionStrings` section to the `appsettings.Development.json` file so that it matches the following code.
115
103
116
104
```json
117
105
{
@@ -128,49 +116,61 @@ Complete the following steps to connect to Azure SQL Database using Entity Frame
128
116
}
129
117
```
130
118
131
-
1. Add the following code to the `Program.cs` file above the line of code that reads `var app = builder.Build();`. This code performs the following configurations:
132
-
133
-
* Retrieves the passwordless database connection string from the `appsettings.Development.json` file for local development, or from the environment variables for hosted production scenarios.
134
-
* Registers the Entity Framework Core `DbContext` class with the .NET dependency injection container.
> Remember to update the `<your database-server-name>` and `<your-database-name>` placeholders in the database connection string. Passwordless connection strings are safe to commit to source control, since they do not contain any secrets such as usernames, passwords, or access keys.
The passwordless connection string includes a configuration value of `Authentication=Active Directory Default`, which enables Entity Framework Core to use `DefaultAzureCredential` to connect to Azure services. When the app runs locally, it authenticates with the user you're signed into Visual Studio with. Once the app deploys to Azure, the same code discovers and applies the managed identity that is associated with the hosted app, which you'll configure later.
151
123
152
-
1. Add the following endpoints to the bottom of the `Program.cs` file above `app.Run()` to retrieve and add entities in the database using the `PersonDbContext` class.
124
+
1. Replace the contents of the `Program.cs` file with the following code:
Finally, add the `Person` and `PersonDbContext` classes to the bottom of the `Program.cs` file. The Person class represents a single record in the database's `Persons` table. The `PersonDbContext` class represents the Person database and allows you to perform operations on it through code. You can read more about `DbContext` in the [Getting Started](/ef/core/get-started/overview/first-app) documentation for Entity Framework Core.
172
+
app.Run();
172
173
173
-
```csharp
174
174
public class Person
175
175
{
176
176
public int Id { get; set; }
@@ -189,6 +189,14 @@ Complete the following steps to connect to Azure SQL Database using Entity Frame
189
189
}
190
190
```
191
191
192
+
The preceding code handles the following:
193
+
194
+
- Retrieves the passwordless database connection string from the `appsettings.Development.json` file for local development, or from the environment variables for hosted production scenarios.
195
+
- Registers the Entity Framework Core `DbContext` class with the .NET dependency injection container. You can read more about `DbContext` in the [Getting Started](/ef/core/get-started/overview/first-app) documentation for Entity Framework Core.
196
+
- Configures .NET 9.0 OpenAPI support with SwaggerUI to provide a UI you can use to interact with the app endpoints and database.
197
+
- Adds endpoints to retrieve and add entities in the database.
198
+
- Defines a `Person` class to represent a single record in the `Persons` database table, and the `PersonDbContext` class that was registered with the .NET dependency injection container.
199
+
192
200
## Run the migrations to create the database
193
201
194
202
To update the database schema to match your data model using Entity Framework Core, you must use a migration. Migrations can create and incrementally update a database schema to keep it in sync with your application's data model. You can learn more about this pattern in the [migrations overview](/ef/core/managing-schemas/migrations).
@@ -252,11 +260,11 @@ The app is ready to be deployed to Azure. Visual Studio can create an Azure App
252
260
1. For the specific target, select **Azure App Service (Windows)**, and then select **Next**.
253
261
1. Select the green **+** icon to create a new App Service to deploy to and enter the following values:
254
262
255
-
* **Name**: Leave the default value.
256
-
* **Subscription name**: Select the subscription to deploy to.
257
-
* **Resource group**: Select **New** and create a new resource group called *msdocs-dotnet-sql*.
258
-
* **Hosting Plan**: Select **New** to open the hosting plan dialog. Leave the default values and select **OK**.
259
-
* Select **Create** to close the original dialog. Visual Studio creates the App Service resource in Azure.
263
+
- **Name**: Leave the default value.
264
+
- **Subscription name**: Select the subscription to deploy to.
265
+
- **Resource group**: Select **New** and create a new resource group called *msdocs-dotnet-sql*.
266
+
- **Hosting Plan**: Select **New** to open the hosting plan dialog. Leave the default values and select **OK**.
267
+
- Select **Create** to close the original dialog. Visual Studio creates the App Service resource in Azure.
260
268
261
269
:::image type="content" source="media/passwordless-connections/create-app-service-small.png" alt-text="Screenshot showing how to deploy with Visual Studio." lightbox="media/passwordless-connections/create-app-service.png":::
262
270
@@ -265,7 +273,7 @@ The app is ready to be deployed to Azure. Visual Studio can create an Azure App
265
273
266
274
1. Select **Publish** in the upper right of the publishing profile summary to deploy the app to Azure.
267
275
268
-
When the deployment finishes, Visual Studio launches the browser to display the hosted app, but at this point the app doesn't work correctly on Azure. You still need to configure the secure connection between the App Service and the SQL database to retrieve your data.
276
+
When the deployment finishes, Visual Studio launches the browser to display the hosted app. You should see the `Hello world` message from the default endpoint. However, at this point the database endpoints will not work correctly on Azure. You still need to configure the secure connection between the App Service and the SQL database to retrieve your data.
269
277
270
278
## Connect the App Service to Azure SQL Database
271
279
@@ -279,17 +287,25 @@ There are multiple tools available to implement these steps:
Service Connector is a tool that streamlines authenticated connections between different services in Azure. Service Connector currently supports connecting an App Service to a SQL database via the Azure CLI using the `az webapp connection create sql` command. This single command completes the three steps mentioned above for you.
290
+
Service Connector is a tool that streamlines authenticated connections between different services in Azure. Service Connector currently supports connecting an App Service to a SQL database using the Azure CLI passwordless extension.
283
291
284
-
```azurecli
285
-
az webapp connection create sql
286
-
-g <your-resource-group>
287
-
-n <your-app-service-name>
288
-
--tg <your-database-server-resource-group>
289
-
--server <your-database-server-name>
290
-
--database <your-database-name>
291
-
--system-identity
292
-
```
292
+
1. Install or upgrade the Service Connector passwordless extension:
293
+
294
+
```azcli
295
+
az extension add --name serviceconnector-passwordless --upgrade
296
+
```
297
+
298
+
1. Run the `az webapp connection create sql` command to connect your web app to the database using a system-assigned managed identity:
299
+
300
+
```azurecli
301
+
az webapp connection create sql
302
+
-g <your-resource-group>
303
+
-n <your-app-service-name>
304
+
--tg <your-database-server-resource-group>
305
+
--server <your-database-server-name>
306
+
--database <your-database-name>
307
+
--system-identity
308
+
```
293
309
294
310
You can verify the changes made by Service Connector on the App Service settings.
295
311
@@ -346,6 +362,9 @@ The person you created locally should display in the browser. Congratulations! Y
0 commit comments