Skip to content

Commit 1f3b436

Browse files
authored
Merge branch 'master' into feature/triggered-db-context-perf
2 parents ef96530 + c24843a commit 1f3b436

28 files changed

+299
-124
lines changed

EntityFrameworkCore.Triggered.Samples.sln

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "2 - PrimarySchool", "sample
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3 - StudentManagerAspNetCore", "samples\3 - StudentManagerAspNetCore\3 - StudentManagerAspNetCore.csproj", "{5EFFD8A3-9DC6-4BB5-A557-EA737B6B134D}"
1111
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "4 - TicketManager", "samples\4 - TicketManager\4 - TicketManager.csproj", "{5280D1D2-EBAD-46A0-AE50-53F540F0E25A}"
13-
EndProject
1412
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FBBD1EA1-974D-40AB-8B78-A1DBEF86D6E0}"
1513
ProjectSection(SolutionItems) = preProject
1614
samples\Directory.Build.props = samples\Directory.Build.props
@@ -34,10 +32,6 @@ Global
3432
{5EFFD8A3-9DC6-4BB5-A557-EA737B6B134D}.Debug|Any CPU.Build.0 = Debug|Any CPU
3533
{5EFFD8A3-9DC6-4BB5-A557-EA737B6B134D}.Release|Any CPU.ActiveCfg = Release|Any CPU
3634
{5EFFD8A3-9DC6-4BB5-A557-EA737B6B134D}.Release|Any CPU.Build.0 = Release|Any CPU
37-
{5280D1D2-EBAD-46A0-AE50-53F540F0E25A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38-
{5280D1D2-EBAD-46A0-AE50-53F540F0E25A}.Debug|Any CPU.Build.0 = Debug|Any CPU
39-
{5280D1D2-EBAD-46A0-AE50-53F540F0E25A}.Release|Any CPU.ActiveCfg = Release|Any CPU
40-
{5280D1D2-EBAD-46A0-AE50-53F540F0E25A}.Release|Any CPU.Build.0 = Release|Any CPU
4135
EndGlobalSection
4236
GlobalSection(SolutionProperties) = preSolution
4337
HideSolutionNode = FALSE

samples/1 - HelloWorld/README.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Hello world
22
A most basic application using triggers. In this example, Students that are being added will be assigned a CreatedOn datetime that is covered by now.
33

4+
## Triggers
5+
6+
- StudentAssignRegistrationDate: This trigger assigns a registration date to newly added users
7+
48
## Build and run this sample
59
Run `dotnet run` in the root of this project

samples/1 - HelloWorld/Triggers/StudentAssignRegistrationDate.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ public class StudentAssignRegistrationDate : IBeforeSaveTrigger<Student>
1212
{
1313
public Task BeforeSave(ITriggerContext<Student> context, CancellationToken cancellationToken)
1414
{
15-
context.Entity.RegistrationDate = DateTime.Today;
15+
if (context.ChangeType == ChangeType.Added)
16+
{
17+
context.Entity.RegistrationDate = DateTime.Today;
18+
}
1619

1720
return Task.CompletedTask;
1821
}

samples/2 - PrimarySchool/ApplicationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class StudentCourse
4141

4242
public class ApplicationContext : TriggeredDbContext
4343
{
44-
public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
44+
public ApplicationContext(DbContextOptions<ApplicationContext> options, IServiceProvider serviceProvider) : base(options, serviceProvider)
4545
{
4646

4747
}

samples/2 - PrimarySchool/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ static void Main(string[] args)
1414
IServiceScope serviceScope = null;
1515

1616
var serviceProvider = new ServiceCollection()
17-
.AddDbContext<ApplicationContext>(options => {
17+
.AddTriggeredDbContext<ApplicationContext>(options => {
1818
options
19-
.UseSqlite("Data source=test.db")
20-
.UseTriggers(triggerOptions => {
21-
triggerOptions.UseApplicationScopedServiceProviderAccessor(_ => serviceScope.ServiceProvider);
22-
});
19+
.UseSqlite("Data source=test.db");
2320
})
2421
.AddScoped<IBeforeSaveTrigger<Student>, Triggers.StudentSignupToMandatoryCourses>()
2522
.BuildServiceProvider();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Hello world
2-
A most basic application using triggers. In this example, Students that are being added will be assigned a CreatedOn datetime that is covered by now.
2+
A sample application showing triggers using dependency injection and causing recursion.
33

44
## Build and run this sample
55
Run `dotnet run` in the root of this project

samples/3 - StudentManagerAspNetCore/3 - StudentManagerAspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="EntityFrameworkCore.Triggered" Version="$(EFCoreTriggeredPackageVersion)" />
11+
<PackageReference Include="EntityFrameworkCore.Triggered.AspNetCore" Version="$(EFCoreTriggeredPackageVersion)" />
1112
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.6" />
1213
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.6" />
1314
</ItemGroup>
Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Security.Cryptography.X509Certificates;
45
using System.Text;
56
using System.Threading.Tasks;
67
using EntityFrameworkCore.Triggered;
78
using Microsoft.EntityFrameworkCore;
89

910
namespace StudentManager
1011
{
11-
public class Student
12-
{
13-
public int Id { get; set; }
14-
15-
public string DisplayName { get; set; }
16-
17-
public DateTimeOffset RegistrationDate { get; set; }
18-
19-
public ICollection<StudentCourse> Courses { get; set; }
20-
}
21-
22-
public class Course
23-
{
24-
public int Id { get; set; }
25-
26-
public string DisplayName { get; set; }
27-
28-
public bool IsMandatory { get; set; }
29-
30-
}
31-
32-
public class StudentCourse
33-
{
34-
public int StudentId { get; set; }
35-
36-
public int CourseId { get; set; }
37-
38-
public Student Student { get; set; }
39-
40-
public Course Course { get; set; }
41-
}
42-
4312

4413
public class ApplicationContext : TriggeredDbContext
4514
{
@@ -53,6 +22,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
5322
modelBuilder.Entity<StudentCourse>()
5423
.HasKey(x => new { x.StudentId, x.CourseId });
5524

25+
modelBuilder.Entity<Audit>()
26+
.HasKey(x => new { x.Discriminator, x.Id, x.RecordDate });
27+
28+
modelBuilder.Entity<Course>()
29+
.HasQueryFilter(x => x.DeletedOn == null);
30+
31+
modelBuilder.Entity<StudentCourse>()
32+
.HasQueryFilter(x => x.Course.DeletedOn == null);
33+
5634
base.OnModelCreating(modelBuilder);
5735
}
5836

@@ -61,5 +39,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
6139
public DbSet<Course> Courses { get; set; }
6240

6341
public DbSet<StudentCourse> StudentCourses { get; set; }
42+
43+
public DbSet<Audit> Audits { get; set; }
6444
}
6545
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using StudentManager.Traits;
6+
7+
namespace StudentManager
8+
{
9+
public class Student : ISoftDelete, IAudited
10+
{
11+
public int Id { get; set; }
12+
13+
public string DisplayName { get; set; }
14+
15+
public DateTimeOffset RegistrationDate { get; set; }
16+
17+
public ICollection<StudentCourse> Courses { get; set; }
18+
19+
public DateTimeOffset? DeletedOn { get; set; }
20+
}
21+
22+
public class Course : ISoftDelete, IAudited
23+
{
24+
public int Id { get; set; }
25+
26+
public string DisplayName { get; set; }
27+
28+
public bool IsMandatory { get; set; }
29+
30+
public DateTimeOffset? DeletedOn { get; set; }
31+
}
32+
33+
public class StudentCourse
34+
{
35+
public int StudentId { get; set; }
36+
37+
public int CourseId { get; set; }
38+
39+
public Student Student { get; set; }
40+
41+
public Course Course { get; set; }
42+
}
43+
44+
public class Audit
45+
{
46+
public string Discriminator { get; set; }
47+
48+
public int Id { get; set; }
49+
50+
public DateTimeOffset RecordDate { get; set; }
51+
52+
public string Record { get; set; }
53+
}
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@page
2+
@model StudentManager.Pages.AuditsModel
3+
4+
@{
5+
ViewData["Title"] = "Audits";
6+
}
7+
8+
<h1>Audits</h1>
9+
10+
<table class="table">
11+
<thead>
12+
<tr>
13+
<th>
14+
@Html.DisplayNameFor(model => model.Audit[0].Discriminator)
15+
</th>
16+
<th>
17+
@Html.DisplayNameFor(model => model.Audit[0].Id)
18+
</th>
19+
<th>
20+
@Html.DisplayNameFor(model => model.Audit[0].RecordDate)
21+
</th>
22+
<th>
23+
@Html.DisplayNameFor(model => model.Audit[0].Record)
24+
</th>
25+
</tr>
26+
</thead>
27+
<tbody>
28+
@foreach (var item in Model.Audit) {
29+
<tr>
30+
<td>
31+
@Html.DisplayFor(modelItem => item.Discriminator)
32+
</td>
33+
<td>
34+
@Html.DisplayFor(modelItem => item.Id)
35+
</td>
36+
<td>
37+
@Html.DisplayFor(modelItem => item.RecordDate)
38+
</td>
39+
<td>
40+
@Html.DisplayFor(modelItem => item.Record)
41+
</td>
42+
</tr>
43+
}
44+
</tbody>
45+
</table>

0 commit comments

Comments
 (0)