Triggers fired twice in Entity Framework 9
We use both AddTriggeredDbContextPool and AddTriggeredDbContextFactory and that has worked while we've been running on .NET 7 & Entity Framework 7.
We've upgraded to .NET 9 and Entity Framework 9 and that has caused the triggers to be executed twice.
Here is the repro:
Code:
services.AddTriggeredDbContextPool<AppContext>(options =>
{
options.UseSqlServer(
$"Server=.;Database={dbName};Trusted_Connection=True;TrustServerCertificate=True;");
options.UseTriggers(opts =>
{
opts.AddAssemblyTriggers(typeof(Program).Assembly);
});
})
.AddTriggeredDbContextFactory<AppContext>(options =>
{
options.UseSqlServer(
$"Server=.;Database={dbName};Trusted_Connection=True;TrustServerCertificate=True;");
options.UseTriggers(opts =>
{
opts.AddAssemblyTriggers(typeof(Program).Assembly);
});
})
If I remove the call to UseTriggers in the AddTriggeredDbContextFactory call, that resolves the issue and the triggers are still fired when a SaveChanges occurs on a DbContext created via the factory.
If I rollback Entity Framework to 8.0.3, the triggers are not executed twice despite UseTriggers being called in both.
It seems that a change in Entity Framework 9 around service registrations is causing the issue.
Happy to help any way I can to resolve the issue.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.