Open DrPhil opened 6 months ago
Can you try and use v4.0.0-preview.1? I couldn't reproduce this issue with that version
I could reproduce this with 4.0.0-preview.1 as well.
Modified csproj
<PackageReference Include="EntityFrameworkCore.Triggered" Version="4.0.0-preview.1" />
Modified Models.cs
public class MyTrigger : IBeforeSaveTrigger<MyEntity>
{
public void BeforeSave(ITriggerContext<MyEntity> context)
{
Console.WriteLine("Here we are in the trigger");
Console.WriteLine($"The unmodified entity is: {context.UnmodifiedEntity}");
}
}
Output
Adding to context
Here we are in the trigger
The unmodified entity is:
Updating context
Here we are in the trigger
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at lambda_method50(Closure, MaterializationContext)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ArrayPropertyValues.ToObject()
at EntityFrameworkCore.Triggered.TriggerContext`1.get_UnmodifiedEntity()
at TriggeredComplexType.MyTrigger.BeforeSave(ITriggerContext`1 context) in /home/simon/Github/TriggeredComplexType/Models.cs:line 42
at EntityFrameworkCore.Triggered.Internal.TriggerTypeDescriptorHelpers.<>c__DisplayClass2_0`2.<GetWeakDelegateHelper>b__0(Object trigger, Object triggerContext)
at EntityFrameworkCore.Triggered.Internal.Descriptors.BeforeSaveTriggerDescriptor.Invoke(Object trigger, Object triggerContext, Exception exception)
at EntityFrameworkCore.Triggered.Internal.TriggerDescriptor.Invoke(Object triggerContext, Exception exception)
at EntityFrameworkCore.Triggered.TriggerSession.RaiseTriggers(Type openTriggerType, Exception exception, ITriggerContextDiscoveryStrategy triggerContextDiscoveryStrategy, Func`2 triggerTypeDescriptorFactory)
at EntityFrameworkCore.Triggered.TriggerSession.RaiseBeforeSaveTriggers(Boolean skipDetectedChanges)
at EntityFrameworkCore.Triggered.TriggerSession.RaiseBeforeSaveTriggers()
at EntityFrameworkCore.Triggered.Internal.TriggerSessionSaveChangesInterceptor.SavingChangesAsync(DbContextEventData eventData, InterceptionResult`1 result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Program.<Main>$(String[] args) in /home/simon/Github/TriggeredComplexType/Program.cs:line 21
at Program.<Main>$(String[] args) in /home/simon/Github/TriggeredComplexType/Program.cs:line 21
at Program.<Main>(String[] args)
I tried with the async version as well, and had the same result. I uploaded the example to a repository so hopefully you can reproduce as well. https://github.com/DrPhil/triggered-complextype-crash
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.
Is there anything else you need from me? Did my example in the repo reproduce for you?
This issue seems to be resolved when targeting EF Core 9. I assume that there was an issue fixed related to this issue as the stacktrace points to something internal to EF Core. I'll leave this issue open for now as we may want to find a way to prevent this issue from happening when targeting EF Core 8 or below
Accessing
UnmodifiedEntity
on a type with a ComplexType on it causes an exception. It doesn't matter if the changed value is inside the complextype or directly on the entity: accessing the unmodified value will crash anyway.Example:
Models.cs
Program.cs
csproj
Output