You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the given example, we register triggers directly with our DbContext. This is the recommended approach starting from version 2.3 and 1.4 respectively. If you're on an older version then its recommend to register triggers with your applications DI container instead:
Doing so will make sure that your triggers can use other services registered in your DI container.
109
+
110
+
You can also use functionality in [EntityFrameworkCore.Triggered.Extensions](https://www.nuget.org/packages/EntityFrameworkCore.Triggered.Extensions/) which allows you to discover triggers that are part of an assembly:
### Cascading changes (previously called Recursion)
95
120
`BeforeSaveTrigger<TEntity>` supports cascading triggers. This is useful since it allows your triggers to subsequently modify the same DbContext entity graph and have it raise additional triggers. By default this behavior is turned on and protected from infinite loops by limiting the number of cascading cycles. If you don't like this behavior or want to change it, you can do so by:
96
121
```csharp
@@ -169,23 +194,6 @@ In this example we were not able to inherit from TriggeredDbContext since we wan
169
194
### Custom trigger types
170
195
Bydefaultweoffer3triggertypes: `IBeforeSaveTrigger`, `IAfterSaveTrigger` and `IAfterSaveFailedTrigger`. Thesewillcovermostcases. Inadditionweoffer `IRaiseBeforeCommitTrigger` and `IRaiseAfterCommitTrigger` asanextensiontofurtherenhanceyourcontrolofwhentriggersshouldrun. Wealsooffersupportforcustomtriggers. Letssaywewanttoreacttospecificeventshappeninginyourcontext. Wecandosobycreatinganewinterface: IThisThingJustHappenedTriggerandimplementinganextensionmethodforITriggerSessiontoinvoketriggersofthattype. Pleasetakealookathow [Transactionaltriggers](https://github.com/koenbeuk/EntityFrameworkCore.Triggered/tree/master/src/EntityFrameworkCore.Triggered.Transactions) are implemented as an example.
171
196
172
-
### When you don't want to use dependency injection
triggerOptions.AddTrigger<BeforeSaveStudentTrigger>(); // Register your triggers
182
-
});
183
-
184
-
base.OnConfiguring(optionsBuilder);
185
-
}
186
-
}
187
-
```
188
-
189
197
### Similar products
190
198
- [Ramses](https://github.com/JValck/Ramses): Lifecycle hooks for EFCore. A simple yet effective way of reacting to changes. Great for situations where you simply want to make sure that a property is set before saving to the database. Limited though in features as there is no dependency injection, no async support, no extensibility model and lifecycle hooks need to be implemented on the entity type itself.
191
199
- [EntityFramework.Triggers](https://github.com/NickStrupat/EntityFramework.Triggers). Add triggers to your entities with insert, update, and delete events. There are three events for each: before, after, and upon failure. A fine alternative to EntityFrameworkCore.Triggered. It has been around for some time and has support for EF6 and boast a decent community. There are plenty of trigger types to opt into including the option to cancel SaveChanges from within a trigger. A big drawback however is that it does not support cascading triggers so that triggers can never be relied on to enforce a domain constraint.
0 commit comments