Skip to content

Commit 7fb1379

Browse files
authored
Update README.md
1 parent 718da89 commit 7fb1379

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ and the order in which triggers are registered, a trigger can also implement the
118118
### Error handling
119119
In some cases, you want to be triggered when a DbUpdateException occurs. For this purpose we have `IAfterSaveFailedTrigger<TEntity>`. This gets triggered for all entities as part of the change set when DbContext.SaveChanges raises a DbUpdateException. The handling method: `AfterSaveFailed` in turn gets called with the trigger context containing the entity as well as the exception. You may attempt to call `DbContext.SaveChanges` again from within this trigger. This will not raise triggers that are already raised and only raise triggers that have since become relevant (based on the cascading configuration).
120120

121+
### Lifecycle triggers
122+
Starting with version 2.1.0, we added support for Lifecycle triggers. These triggers are invoked once per trigger type per SaveChanges lifecyle and reside within the `EntityFrameworkCore.Triggered.Lifecycles` namespace. These can be used to run something before/after all individual triggers have run. Consider the following example:
123+
```csharp
124+
public BulkReportTrigger : IAfterSaveTrigger<Email>, IAfterSaveCompletedTrigger {
125+
private List<string> _emailAddresses = new List<string>();
126+
127+
// This may be invoked multiple times within the same SaveChanges call if there are multiple emails
128+
public Task AfterSave(ITriggerContext<Email> context, CancellationToken cancellationToken) {
129+
if (context.ChangeType == ChangeType.Added) {
130+
this._emailAddresses.Add(context.Address);
131+
return Task.CompletedTask;
132+
}
133+
}
134+
135+
public Task AfterSaveCompleted(CancellationToken cancellationToken) {
136+
Console.WriteLine($"We've sent {_emailAddresses.Count()} emails to {_emailAddresses.Distinct().Count()}" distinct email addresses");
137+
return Task.CompletedTask;
138+
}
139+
}
140+
```
141+
121142
### Transactions
122143
Many database providers support the concept of a Transaction. By default when using SqlServer with EntityFrameworkCore, any call to SaveChanges will be wrapped in a transaction. Any changes made in `IBeforeSaveTrigger<TEntity>` will be included within the transaction and changes made in `IAfterSaveTrigger<TEntity>` will not. However, it is possible for the user to [explicitly control transactions](https://docs.microsoft.com/en-us/ef/core/saving/transactions). Triggers are extensible and one such extension are [Transactional Triggers](https://www.nuget.org/packages/EntityFrameworkCore.Triggered.Transactions/). In order to use this plugin you will have to implement a few steps:
123144
```csharp

0 commit comments

Comments
 (0)