Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SensitiveDataLogging with ComplexProperty: Unable to cast RuntimeComplexType to type IEntityType #32198

Closed
sakrut opened this issue Oct 31, 2023 · 0 comments
Assignees
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@sakrut
Copy link

sakrut commented Oct 31, 2023

Hi, I found this while testing the new ComplexType

Whe i have turn on EnableSensitiveDataLogging and LogTo:

protected override void OnConfiguring(DbContextOptionsBuilder options)
.......
options.EnableSensitiveDataLogging();
options.LogTo(log =>
{
   ......
});

and then i use ComplexType on a List inside ComplexType :

 builder.ComplexProperty(p => p.Information!, mBuilder =>
 {
     mBuilder.Property(mi => mi.Name);
     mBuilder.ComplexProperty(mi => mi.Conditions);  //List<class>

 });

or if i convert this property to json string:

 builder.ComplexProperty(p => p.Information!, mBuilder =>
 {
     mBuilder.Property(mi => mi.Name);
     mBuilder.Property(mi => mi.Conditions).HasJsonConversion();

 });

...
public static ComplexTypePropertyBuilder<T> HasJsonConversion<T>(this ComplexTypePropertyBuilder<T> propertyBuilder) where T : class, new()
{
    ValueConverter<T, string> converter = new(
        v => JsonConvert.SerializeObject(v) ?? string.Empty,
        v => string.IsNullOrEmpty(v) ? new T() : JsonConvert.DeserializeObject<T>(v) ?? new T()
    );

    ValueComparer<T> comparer = new(
        (l, r) => JsonConvert.SerializeObject(l) == JsonConvert.SerializeObject(r),
        v => v == null ? 0 : JsonConvert.SerializeObject(v).GetHashCode(),
        v => JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(v)) ?? new T()
    );

    propertyBuilder.HasConversion(converter);
    propertyBuilder.Metadata.SetValueConverter(converter);
    propertyBuilder.Metadata.SetValueComparer(comparer);
    propertyBuilder.IsRequired(false);

    return propertyBuilder;
}

The cast exception appear during commit add entity transaction.

stack trace

Unable to cast object of type 'Microsoft.EntityFrameworkCore.Metadata.RuntimeComplexType' to type 'Microsoft.EntityFrameworkCore.Metadata.IEntityType'.
      at Microsoft.EntityFrameworkCore.Diagnostics.CoreLoggerExtensions.PropertyChangeDetectedSensitive(EventDefinitionBase definition, EventData payload)
   at Microsoft.EntityFrameworkCore.Diagnostics.EventData.ToString()
   at Microsoft.EntityFrameworkCore.Diagnostics.Internal.FormattingDbContextLogger.Log(EventData eventData)
   at Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger.DispatchEventData(EventDefinitionBase definition, EventData eventData, Boolean diagnosticSourceEnabled, Boolean simpleLogEnabled)
   at Microsoft.EntityFrameworkCore.Diagnostics.CoreLoggerExtensions.PropertyChangeDetectedSensitive(IDiagnosticsLogger`1 diagnostics, InternalEntityEntry internalEntityEntry, IProperty property, Object oldValue, Object newValue)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.LogChangeDetected(InternalEntityEntry entry, IProperty property, Object original, Object current)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectValueChange(InternalEntityEntry entry, IProperty property)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.LocalDetectChanges(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectChanges(IStateManager stateManager)
   at Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.DetectChanges()
   at Microsoft.EntityFrameworkCore.DbContext.TryDetectChanges()
   at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext()
   at OwnComplex.Domain.Service.PeopleService.<AddThinPerson>d__4.MoveNext() in C:\Git2\dotNet\OwnComplex\OwnComplex.Domain\Service\PeopleService.cs:line 32
   at OwnComplex.Domain.Service.ExampleService.<Example2HiddenId>d__3.MoveNext() in C:\Git2\dotNet\OwnComplex\OwnComplex.Domain\Service\ExampleService.cs:line 49
   at OwnComplex.EF8.Worker.<Example2HiddenId>d__5.MoveNext() in C:\Git2\dotNet\OwnComplex\OwnComplex.EF8\Worker.cs:line 47
   at OwnComplex.EF8.Worker.<ExecuteAsync>d__3.MoveNext() in C:\Git2\dotNet\OwnComplex\OwnComplex.EF8\Worker.cs:line 22
   at Microsoft.Extensions.Hosting.Internal.Host.<<StartAsync>b__15_1>d.MoveNext()
   at Microsoft.Extensions.Hosting.Internal.Host.<ForeachService>d__18`1.MoveNext()
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>g__LogAndRethrow|15_3(<>c__DisplayClass15_0& )
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__15.MoveNext()
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
   at Program.<<Main>$>d__0.MoveNext() in C:\Git2\dotNet\OwnComplex\OwnComplex.EF8\Program.cs:line 37
   at Program.<Main>(String[] args)

EF Core version: 8.0.0-rc.2.23480.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: net8.0

IDE: Visual Studio Professional 2022 (64-bit) - Preview Version 17.8.0 Preview 3.0

@ajcvickers ajcvickers self-assigned this Nov 1, 2023
@ajcvickers ajcvickers added this to the 8.0.x milestone Nov 1, 2023
ajcvickers added a commit that referenced this issue Nov 13, 2023
…e data logging is on

Fixes #32198

### Description

Property instances can now belong to complex types, which is a new feature in 8.0. However, logging of a change made to such a property incorrectly assumes the property must be on an entity type.

### Customer impact

Exception when using new feature in 8.0 when sensitive data logging is turned on, which is common.

### How found

Customer reported on 8.0 rc

### Regression

No; complex types are a new feature in 8.0

### Testing

Added tests.

### Risk

Very low; fixing an invalid cast.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 13, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.1 Nov 14, 2023
guimabdo added a commit to cblx/efcore.dataverse that referenced this issue Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

2 participants