Skip to content

Commit c1dbd01

Browse files
Merge pull request #217274 from alexwolfmsft/fix-migration
fixed connection string instructions
2 parents 7fab812 + 9c9b923 commit c1dbd01

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

articles/app-service/tutorial-dotnetcore-sqldb-app.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ To see the entirety of the command output, drop the `--query` in the command.
174174

175175
## 5 - Generate the Database Schema
176176

177-
To generate our database schema, set up a firewall rule on the SQL database server. This rule lets your local computer connect to Azure. For this step, you'll need to know your local computer's IP address. For more information about how to find the IP address, [see here](https://whatismyipaddress.com/).
177+
To generate our database schema, set up a firewall rule on the SQL database server. This rule lets your local computer connect to Azure. For this step, you'll need to know your local computer's IP address. Azure will attempt to detect your IP automatically and presents the option to add it for you, as seen in the steps below. For more information about how to find your IP address manually, [see here](https://whatismyipaddress.com/).
178178

179179
### [Azure portal](#tab/azure-portal)
180180

@@ -195,26 +195,28 @@ az sql server firewall-rule create --resource-group msdocs-core-sql --server <yo
195195

196196
---
197197

198-
Next, update the *appsettings.json* file in the sample project with the [connection string Azure SQL Database](#4---connect-the-app-to-the-database). The update allows us to run migrations locally against our database hosted in Azure. Replace the username and password placeholders with the values you chose when creating your database.
198+
Next, update the name of the connection string in `appsettings.json` file to match the `AZURE_SQL_CONNECTION` name generated by the service connector. When the app is deployed to Azure, the `localdb` connection string value will be overridden by the connection string stored in Azure.
199199

200200
```json
201-
"AZURE_SQL_CONNECTIONSTRING": "Data Source=<your-server-name>.database.windows.net,1433;Initial Catalog=coreDb;User ID=<username>;Password=<password>"
201+
"ConnectionStrings": {
202+
"AZURE_SQL_CONNECTION": "Server=(localdb)\\mssqllocaldb;Trusted_Connection=True;MultipleActiveResultSets=true"
203+
}
202204
```
203205

204-
Next, update the *Startup.cs* file the sample project by updating the existing connection string name `MyDbConnection` to `AZURE_SQL_CONNECTIONSTRING`:
206+
Next, update the `Startup.cs` file the sample project by updating the existing connection string name `MyDbConnection` to `AZURE_SQL_CONNECTIONSTRING`. This change configures the `DbContext` to use the correct connection string in Azure and locally from the `appsettings.json` file.
205207

206208
```csharp
207209
services.AddDbContext<MyDatabaseContext>(options =>
208210
options.UseSqlServer(Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING")));
209211
```
210212

211-
From a local terminal, run the following commands to install the necessary CLI tools for Entity Framework Core, create an initial database migration file, and apply those changes to update the database:
213+
From a local terminal, run the following commands to install the necessary CLI tools for Entity Framework Core, create an initial database migration file, and apply those changes to update the database. Make sure to pass in the connection string value you copied from the Azure SQL database for the `connection` parameter. The `connection` parameter overrides the value of the connection string that is configured for the `DbContext` in the `startup.cs` file.
212214

213215
```dotnetcli
214216
cd <sample-root>\DotNetCoreSqlDb
215217
dotnet tool install -g dotnet-ef
216218
dotnet ef migrations add InitialCreate
217-
dotnet ef database update
219+
dotnet ef database update --connection "<your-azure-sql-connection-string>"
218220
```
219221

220222
After the migration finishes, the correct schema is created.
@@ -223,7 +225,7 @@ If you receive the error `Client with IP address xxx.xxx.xxx.xxx is not allowed
223225

224226
## 6 - Deploy to the App Service
225227

226-
That we're able to create the schema in the database means that our .NET app can connect to the Azure database successfully with the new connection string. Remember that the service connector already configured the `AZURE_SQL_CONNECTIONSTRING` connection string in our App Service app. We're now ready to deploy our .NET app to the App Service.
228+
That we're able to create the schema in the database means that our .NET app can connect to the Azure database successfully with the new connection string. Remember that the service connector already configured the `AZURE_SQL_CONNECTIONSTRING` connection string in our App Service app. We're now ready to deploy our .NET app to the App Service. When the app is deployed, the `AZURE_SQL_CONNECTION` configuration applied to the App Service by the Service Connector will override the `localdb` connection string with the same name in the `appsettings.json` file.
227229

228230
### [Deploy using Visual Studio](#tab/visualstudio-deploy)
229231

0 commit comments

Comments
 (0)