-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
Fixes #19650
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,11 @@ | |
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Query.Internal; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
|
@@ -123,7 +125,26 @@ protected virtual LambdaExpression GenerateMaterializationCondition([NotNull] IE | |
Convert(discriminatorValueVariable, typeof(object)))), | ||
Constant(null, typeof(IEntityType))); | ||
|
||
expressions.Add(Switch(discriminatorValueVariable, exception, switchCases)); | ||
|
||
var discriminatorComparer = discriminatorProperty.GetKeyValueComparer(); | ||
if (discriminatorComparer.IsDefault()) | ||
{ | ||
expressions.Add(Switch(discriminatorValueVariable, exception, switchCases)); | ||
} | ||
else | ||
{ | ||
var staticComparer = typeof(StaticDiscriminatorComparer<,,>).MakeGenericType( | ||
discriminatorProperty.DeclaringEntityType.ClrType, | ||
discriminatorProperty.ClrType, | ||
discriminatorProperty.GetTypeMapping().Converter.ProviderClrType); | ||
|
||
var comparerField = staticComparer.GetField(nameof(StaticDiscriminatorComparer<int, int, int>.Comparer)); | ||
comparerField.SetValue(null, discriminatorComparer); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ajcvickers
Author
Member
|
||
|
||
var equalsMethod = staticComparer.GetMethod(nameof(StaticDiscriminatorComparer<int, int, int>.DiscriminatorEquals)); | ||
expressions.Add(Switch(discriminatorValueVariable, exception, equalsMethod, switchCases)); | ||
} | ||
|
||
body = Block(new[] { discriminatorValueVariable }, expressions); | ||
} | ||
else | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
// ReSharper disable twice UnusedTypeParameter | ||
public static class StaticDiscriminatorComparer<TEntity, TModel, TProvider> | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public static ValueComparer<TModel> Comparer; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public static bool DiscriminatorEquals([CanBeNull] TModel x, [CanBeNull] TModel y) | ||
=> Comparer.Equals(x, y); | ||
} | ||
} |
This won't work. You are setting it during compilation, so the next query using a different comparer will override it.
You need to create an expression that does this before the switch.