As your tracked entity and change entity will require most of the same properties, it is recommended to create a base class that both will extend. See the samples for an implementation of this.
- Add the below nuget package to your project:
EFCore.ChangeTriggers.SqlServer - Implement the
ITracked<>interface on any entity classes that you want to track:public class User : ITracked<UserChange> { ... - Create a change entity for the tracked entity that implements the
IChange<>interface:public class UserChange : IChange<User> { ... - Add the below assembly attribute to your
Program.cs:[assembly: DesignTimeServicesReference("EFCore.ChangeTriggers.ChangeTriggersDesignTimeServices, EFCore.ChangeTriggers")] - Enable ChangeTriggers on your
DbContext:services.AddDbContext<MyDbContext>(options => { options .UseSqlServer() .UseSqlServerChangeTriggers(); }); - Auto-configure your change trigger entities in the
OnModelCreating()method of yourDbContext:protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.AutoConfigureChangeTriggers(); ... - Create a migration to generate the required objects:
dotnet ef migrations add ChangeTriggers