Skip to content

Commit 1fa6163

Browse files
authored
Merge pull request dodyg#246 from koskila/item/azurefunctions-dbcontext-sample
Item/azurefunctions dbcontext sample
2 parents cc8b6da + 100fb4d commit 1fa6163

File tree

13 files changed

+593
-4
lines changed

13 files changed

+593
-4
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 320 Samples for ASP.NET Core 3.1.300 and ASP.NET Core 5 RC2
1+
# 324 Samples for ASP.NET Core 3.1.300 and ASP.NET Core 5 RC2
22

33
## People of Beirut needs our help. [Please support them](https://lebanoncrisis.carrd.co/#donate).
44

@@ -35,9 +35,9 @@ Hi Nuget visitors, if you have problem finding the sample you are looking for, p
3535
| [Generic Hosting](/projects/generic-host) | (9) | [Uri Helper](/projects/uri-helper) | (5) |
3636
| [gRPC](/projects/grpc) (including grpc-Web) | (12) | [Web Sockets](/projects/web-sockets) | (5) |
3737
| [Logging](/projects/logging) | (2) | [Web Utilities](/projects/web-utilities) | (3) |
38-
| [Localization and Globalization](projects/localization) | (6) | [Azure Functions](/projects/azure-functions) | (1) |
38+
| [Localization and Globalization](projects/localization) | (6) | [Azure Functions](/projects/azure-functions) | (2) |
3939
| [Single File Application](projects/5-0/sfa) - C# 9, ASP.NET Core 5| (1) | For Data Access samples, go to the excellent [ORM Cookbook](https://github.com/Grauenwolf/DotNet-ORM-Cookbook). | |
40-
| | 125 | | 143 |
40+
| | 138 | | 144 |
4141
## How to run these samples
4242

4343
To run these samples, simply open your command line console, go to each folder and execute `dotnet watch run`.
@@ -154,9 +154,17 @@ The samples in this section rely on [Wangkanai.Detection](https://github.com/wan
154154

155155
This example shows how to enable image resizing functionality to your site. It's super easy and the middleware takes care of caching, etc.
156156

157-
### Azure Functions (1)
157+
### Azure Functions (2)
158+
159+
These samples show you how to achieve different scenarios without out-of-the-box templates, such as referencing an Entity Framework Core database context in the function.
160+
161+
* [How to modify Startup.cs in an Azure Function?](/projects/azure-functions/StartupExample)
158162

159163
The sample shows how you can add your own code to the startup of the Azure Functions host by adding a custom Startup class.
164+
165+
* [How to include an EF Core dbcontext in an Azure Function?](/projects/azure-functions/DbContextExample)
166+
167+
The sample shows an example on how to include an Entity Framework Core database context in your Azure Function, and access entities in it.
160168

161169
## Misc
162170

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using DataLayer.Entities;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace DataLayer
5+
{
6+
public class ApplicationDbContext : DbContext
7+
{
8+
public DbSet<Entity> Entities { get; set; }
9+
10+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
11+
{
12+
}
13+
14+
public ApplicationDbContext(DbContextOptions options) : base(options)
15+
{
16+
}
17+
18+
public ApplicationDbContext() : base()
19+
{
20+
}
21+
22+
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=mydb;Trusted_Connection=True;MultipleActiveResultSets=True");
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<!--<SkipFunctionsDepsCopy>true</SkipFunctionsDepsCopy>-->
6+
</PropertyGroup>
7+
8+
<ItemGroup>
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+
15+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Text;
5+
6+
namespace DataLayer.Entities
7+
{
8+
public class Entity
9+
{
10+
[Key]
11+
public int Id { get; set; }
12+
13+
public string Name { get; set; }
14+
15+
public string Description { get; set; }
16+
}
17+
}

projects/azure-functions/DbContextExample/Data/Migrations/20201020183453_Initial.Designer.cs

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
namespace DataLayer.Migrations
4+
{
5+
public partial class Initial : Migration
6+
{
7+
protected override void Up(MigrationBuilder migrationBuilder)
8+
{
9+
migrationBuilder.CreateTable(
10+
name: "Entities",
11+
columns: table => new
12+
{
13+
Id = table.Column<int>(nullable: false)
14+
.Annotation("SqlServer:Identity", "1, 1"),
15+
Name = table.Column<string>(nullable: true),
16+
Description = table.Column<string>(nullable: true)
17+
},
18+
constraints: table =>
19+
{
20+
table.PrimaryKey("PK_Entities", x => x.Id);
21+
});
22+
}
23+
24+
protected override void Down(MigrationBuilder migrationBuilder)
25+
{
26+
migrationBuilder.DropTable(
27+
name: "Entities");
28+
}
29+
}
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// <auto-generated />
2+
using DataLayer;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Metadata;
6+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
7+
8+
namespace DataLayer.Migrations
9+
{
10+
[DbContext(typeof(ApplicationDbContext))]
11+
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
12+
{
13+
protected override void BuildModel(ModelBuilder modelBuilder)
14+
{
15+
#pragma warning disable 612, 618
16+
modelBuilder
17+
.HasAnnotation("ProductVersion", "3.1.0")
18+
.HasAnnotation("Relational:MaxIdentifierLength", 128)
19+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
20+
21+
modelBuilder.Entity("DataLayer.Entities.Entity", b =>
22+
{
23+
b.Property<int>("Id")
24+
.ValueGeneratedOnAdd()
25+
.HasColumnType("int")
26+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
27+
28+
b.Property<string>("Description")
29+
.HasColumnType("nvarchar(max)");
30+
31+
b.Property<string>("Name")
32+
.HasColumnType("nvarchar(max)");
33+
34+
b.HasKey("Id");
35+
36+
b.ToTable("Entities");
37+
});
38+
#pragma warning restore 612, 618
39+
}
40+
}
41+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30309.148
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataLayer", "Data\DataLayer.csproj", "{30BD593E-8752-4661-9FCB-E97AD112ED4C}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Function", "Function\Function.csproj", "{E8020AA9-4E33-4049-8B8A-0B4965AEA3F6}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{30BD593E-8752-4661-9FCB-E97AD112ED4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{30BD593E-8752-4661-9FCB-E97AD112ED4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{30BD593E-8752-4661-9FCB-E97AD112ED4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{30BD593E-8752-4661-9FCB-E97AD112ED4C}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{E8020AA9-4E33-4049-8B8A-0B4965AEA3F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{E8020AA9-4E33-4049-8B8A-0B4965AEA3F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{E8020AA9-4E33-4049-8B8A-0B4965AEA3F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{E8020AA9-4E33-4049-8B8A-0B4965AEA3F6}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {0B791877-6A94-4F1E-B3BB-C425B9B66B27}
30+
EndGlobalSection
31+
EndGlobal

0 commit comments

Comments
 (0)