Running App Service with Azure SQL based
Fix list:
The steps in this guide has been tested with “.NET 6.0”
EFdotnet tool install -g dotnet-ef dotnet ef migrations add InitialCreate dotnet ef database update
Adding packagesdotnet add package Microsoft.Data.SqlClient --version 4.0.1 dotnet add package Azure.Identity --version 1.5.0
appsettings.json :
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"CoredBConnection": "Server=tcp:mydatabase-server-sqlsrv.database.windows.net;Authentication=Active Directory Default; Database=CoredB;"
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"CoredBConnection": "Server=tcp:mydatabase-server-sqlsrv.database.windows.net;Authentication=Active Directory Default; Database=CoredB;"
}
}
{ "Logging": { "LogLevel": { "Default": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "CoredBConnection": "Server=tcp:mydatabase-server-sqlsrv.database.windows.net;Authentication=Active Directory Default; Database=CoredB;" } }
az webapp identity assign --resource-group my-002-rg --name webapp-core-sql-018
Create database userCREATE USER [webapp-core-sql-018] FROM EXTERNAL PROVIDER; ALTER ROLE db_datareader ADD MEMBER [webapp-core-sql-018]; ALTER ROLE db_datawriter ADD MEMBER [webapp-core-sql-018]; ALTER ROLE db_ddladmin ADD MEMBER [webapp-core-sql-018]; GO
Program.cs :
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MyDatabaseContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CoredBConnection")));
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MyDatabaseContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CoredBConnection")));
}
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddDbContext<MyDatabaseContext>(options => options.UseSqlServer(Configuration.GetConnectionString("CoredBConnection"))); }
See Also:
- https://docs.microsoft.com/en-us/azure/app-service/tutorial-connect-msi-sql-database
- https://docs.microsoft.com/en-us/azure/app-service/tutorial-dotnetcore-sqldb-app
- https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-6.0&tabs=visual-studio-code
Web App & Azure SQL Templates:
- https://docs.microsoft.com/en-us/azure/azure-sql/database/arm-templates-content-guide?tabs=single-database
- https://docs.microsoft.com/en-us/azure/app-service/samples-resource-manager-templates
- https://azure.microsoft.com/en-au/resources/templates/web-app-sql-database/