Skip to content

Commit 4016f5d

Browse files
committed
Added EF7 Bulk example
1 parent 43fb98a commit 4016f5d

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed

EF7Bulk/EF7Bulk.csproj

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-rc.2.22420.3" />
13+
</ItemGroup>
14+
15+
</Project>

EF7Bulk/EF7Bulk.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF7Bulk", "EF7Bulk.csproj", "{08494CB5-7534-4A75-90C0-43E9EDC4C079}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{08494CB5-7534-4A75-90C0-43E9EDC4C079}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{08494CB5-7534-4A75-90C0-43E9EDC4C079}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{08494CB5-7534-4A75-90C0-43E9EDC4C079}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{08494CB5-7534-4A75-90C0-43E9EDC4C079}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

EF7Bulk/NuGet.config

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="dotnet7" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json" />
5+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
6+
</packageSources>
7+
</configuration>

EF7Bulk/PeopleContext.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace EF7Bulk;
4+
5+
public class PeopleContext : DbContext
6+
{
7+
public DbSet<Person> People { get; set; } = default!;
8+
9+
public PeopleContext(DbContextOptions options) : base(options)
10+
{
11+
}
12+
13+
protected override void OnModelCreating(ModelBuilder modelBuilder)
14+
{
15+
modelBuilder.Entity<Person>().HasKey(p => p.Id);
16+
modelBuilder.Entity<Person>().Property(p => p.Name)
17+
.HasMaxLength(1024)
18+
.IsUnicode(false);
19+
}
20+
}

EF7Bulk/Person.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace EF7Bulk;
2+
3+
public class Person
4+
{
5+
public Person(int id, string name, int age)
6+
{
7+
Id = id;
8+
Name = name;
9+
Age = age;
10+
}
11+
12+
public int Id { get; set; }
13+
public string Name { get; set; }
14+
public int Age { get; set; }
15+
}

EF7Bulk/Program.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Running;
3+
using EF7Bulk;
4+
using Microsoft.Data.Sqlite;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
BenchmarkRunner.Run<UpdateBenchmark>();
8+
9+
[MemoryDiagnoser]
10+
public class UpdateBenchmark
11+
{
12+
private PeopleContext _peopleContext;
13+
14+
[Params(100, 1_000)] public int RowsToUpdate { get; set; }
15+
16+
[GlobalSetup]
17+
public void Setup()
18+
{
19+
_peopleContext = CreateContext();
20+
CreateContext();
21+
FillWithData();
22+
}
23+
24+
[Benchmark(Baseline = true)]
25+
public async Task OneByOneUpdate()
26+
{
27+
var entries = await _peopleContext.People.ToListAsync();
28+
foreach (var entry in entries)
29+
{
30+
entry.Age = 21;
31+
}
32+
33+
await _peopleContext.SaveChangesAsync();
34+
}
35+
36+
[Benchmark]
37+
public async Task BulkUpdate()
38+
{
39+
await _peopleContext.People
40+
.ExecuteUpdateAsync(s => s.SetProperty(p => p.Age, p => 21));
41+
}
42+
43+
private void FillWithData()
44+
{
45+
using var context = CreateContext();
46+
context.Database.EnsureCreated();
47+
for (var i = 1; i <= RowsToUpdate; i++)
48+
context.People.Add(new Person(i, "Steven", 31));
49+
50+
context.SaveChanges();
51+
}
52+
53+
private static PeopleContext CreateContext()
54+
{
55+
var connection = new SqliteConnection("DataSource=myshareddb;mode=memory;cache=shared");
56+
connection.Open();
57+
var options = new DbContextOptionsBuilder()
58+
.UseSqlite(connection)
59+
.Options;
60+
return new PeopleContext(options);
61+
}
62+
}

0 commit comments

Comments
 (0)