Skip to content

Commit 883dd23

Browse files
committed
Includes the database context in the Azure Function
1 parent f0b79e9 commit 883dd23

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

projects/azure-functions/DbContextExample/Function/Function.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netcoreapp3.1</TargetFramework>
44
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
5+
<SkipFunctionsDepsCopy>true</SkipFunctionsDepsCopy>
56
</PropertyGroup>
67
<ItemGroup>
78
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.9" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.9" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.9" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\Data\DataLayer.csproj" />
816
</ItemGroup>
917
<ItemGroup>
1018
<None Update="host.json">
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Function;
2+
using Microsoft.Azure.WebJobs;
3+
using Microsoft.Azure.WebJobs.Hosting;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using DataLayer;
8+
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using System;
11+
using System.IO;
12+
using System.Linq;
13+
using Microsoft.Azure.WebJobs;
14+
using Microsoft.Azure.WebJobs.Hosting;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.Extensions.Logging;
17+
using Microsoft.EntityFrameworkCore;
18+
using Microsoft.Extensions.Configuration;
19+
20+
[assembly: WebJobsStartup(typeof(StartUp))]
21+
namespace Function
22+
{
23+
public class StartUp : IWebJobsStartup
24+
{
25+
public void Configure(IWebJobsBuilder builder)
26+
{
27+
var config = new ConfigurationBuilder()
28+
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
29+
.AddEnvironmentVariables()
30+
.Build();
31+
32+
builder.Services.AddDbContext<ApplicationDbContext>(options1 =>
33+
{
34+
options1.UseSqlServer(
35+
config["ConnectionStrings:DefaultConnection"],
36+
builder =>
37+
{
38+
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
39+
builder.CommandTimeout(10);
40+
}
41+
);
42+
});
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)