EntityFramework.Triggers icon indicating copy to clipboard operation
EntityFramework.Triggers copied to clipboard

GlobalUpdateFailed not triggered when there are more than 1 entries in the change tracker

Open soniachikh opened this issue 4 years ago • 2 comments

We are using .netcore 2.2 We are adding a trigger to the updating action and another trigger (GlobalUpdateFailed) to undo what the updating trigger does when the DB Save fails.  We try to attempt a DB save with a read only dbuser it throws a DBUpdateException but when RaiseFailedEventsAsync is called there is a check for the number of entries in the changetracker and if it is not 1 it exits without calling the GlobalUpdateFailed trigger.   Can you please help us understand the reason for the check.

In our application, we try to make our DB changes transactional, in this one action we indeed change a few DB entries but we only call DB save once to ensure the DB integrity and increase performance. 

soniachikh avatar Feb 08 '21 19:02 soniachikh

Do you have a minimal sample project which reproduces the issue? You can post it on this issue.

NickStrupat avatar Feb 25 '21 19:02 NickStrupat

The following method located in "EntityFramework.Triggers/src/EntityFrameworkCore.Triggers/TriggerInvokerAsync.cs" returns false instead of await RaiseFailedEventsInternalAsync(context, serviceProvider, entries, dbUpdateException); when the number of entries is not 1. However our entries are greater than 1. Therefore we were wondering why you are checking the number of entries since it would be important for us to have the revert for entries that are greater than 1.

public async Task<Boolean> RaiseFailedEventsAsync(DbContext dbContext, IServiceProvider serviceProvider, DbUpdateException dbUpdateException)
		{
			var context = (TDbContext)dbContext;

			IEnumerable<EntityEntry> entries;

			if (dbUpdateException.Entries.Any())
			{
				entries = dbUpdateException.Entries;
			}
			else
			{
				entries = dbContext.ChangeTracker.Entries().ToArray();
				if (entries.Count() != 1)
				{
					return false;
				}
			}
			return await RaiseFailedEventsInternalAsync(context, serviceProvider, entries, dbUpdateException);
		}

jeremy-lafond avatar Feb 25 '21 20:02 jeremy-lafond