Skip to content

Commit d45b3af

Browse files
committed
Added fix for issue in "FindComplexProperty" (dotnet/efcore#32437)
1 parent 4779fff commit d45b3af

File tree

11 files changed

+116
-4
lines changed

11 files changed

+116
-4
lines changed

src/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Sources/Extensions/EntityTypeBuilderExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,13 @@ private static void AddConverterToNonNavigation(
254254
return;
255255

256256
#if COMPLEX_TYPES
257+
#if USE_FIND_COMPLEX_PROPERTY_FIX
258+
var complexProperty = entity.FindComplexPropertyFix(propertyInfo);
259+
#else
260+
var complexProperty = entity.FindComplexProperty(propertyInfo);
261+
#endif
257262
// wil be handled by AddConverterForScalarProperties
258-
if (entity.FindComplexProperty(propertyInfo) is not null)
263+
if (complexProperty is not null)
259264
return;
260265
#endif
261266

@@ -286,8 +291,11 @@ private static void AddNonKeyedValueObjectMembers(
286291
foreach (var memberName in members)
287292
{
288293
#if COMPLEX_TYPES
294+
#if USE_FIND_COMPLEX_PROPERTY_FIX
295+
var complexProperty = entity.FindComplexPropertyFix(memberName);
296+
#else
289297
var complexProperty = entity.FindComplexProperty(memberName);
290-
298+
#endif
291299
if (complexProperty is not null)
292300
continue;
293301
#endif
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if COMPLEX_TYPES
2+
3+
using System.Reflection;
4+
using Microsoft.EntityFrameworkCore.Metadata;
5+
6+
namespace Thinktecture;
7+
8+
internal static class MutableTypeBaseExtensions
9+
{
10+
public static IMutableComplexProperty? FindComplexPropertyFix(
11+
this IMutableTypeBase type,
12+
MemberInfo memberInfo)
13+
{
14+
return type switch
15+
{
16+
IReadOnlyEntityType entityType => (IMutableComplexProperty?)entityType.FindComplexProperty(memberInfo),
17+
IReadOnlyComplexType complexType => (IMutableComplexProperty?)complexType.FindComplexProperty(memberInfo),
18+
_ => type.FindComplexProperty(memberInfo)
19+
};
20+
}
21+
}
22+
23+
#endif

src/Thinktecture.Runtime.Extensions.EntityFrameworkCore8/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Description>Extends Entity Framework Core to support some components from Thinktecture.Runtime.Extensions.</Description>
55
<PackageTags>smart-enum;value-object;discriminated-union;EntityFrameworkCore</PackageTags>
6-
<DefineConstants>$(DefineConstants);COMPLEX_TYPES</DefineConstants>
6+
<DefineConstants>$(DefineConstants);COMPLEX_TYPES;USE_FIND_COMPLEX_PROPERTY_FIX;</DefineConstants>
77
<TargetFramework>net8.0</TargetFramework>
88
</PropertyGroup>
99

src/Thinktecture.Runtime.Extensions.EntityFrameworkCore9/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Description>Extends Entity Framework Core to support some components from Thinktecture.Runtime.Extensions.</Description>
55
<PackageTags>smart-enum;value-object;discriminated-union;EntityFrameworkCore</PackageTags>
6-
<DefineConstants>$(DefineConstants);COMPLEX_TYPES</DefineConstants>
6+
<DefineConstants>$(DefineConstants);COMPLEX_TYPES;USE_FIND_COMPLEX_PROPERTY_FIX;</DefineConstants>
77
<TargetFramework>net8.0</TargetFramework>
88
</PropertyGroup>
99

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/Extensions/EntityTypeBuilderExtensionsTests/AddValueObjectConverters.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ public void Should_add_converters_for_complex_types_inside_complex_value_object(
6868

6969
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
7070
}
71+
72+
[Fact]
73+
public void Should_add_converters_for_complex_value_object_as_complex_type()
74+
{
75+
var entityType = _ctx.Model.FindEntityType(typeof(TestEntityWithComplexValueObjectAsComplexType)) ?? throw new Exception("Entity not found");
76+
var complexProperty = entityType.FindComplexProperty(nameof(TestEntityWithComplexValueObjectAsComplexType.TestComplexType)) ?? throw new Exception("Complex type property not found");
77+
78+
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexValueObject.TestEnum));
79+
}
7180
#endif
7281

7382
[Fact]

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/Extensions/ModelBuilderExtensionsTests/AddValueObjectConverters.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public void Should_add_converters_for_complex_types_inside_complex_value_object(
5555

5656
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
5757
}
58+
59+
[Fact]
60+
public void Should_add_converters_for_complex_value_object_as_complex_type()
61+
{
62+
var entityType = _ctx.Model.FindEntityType(typeof(TestEntityWithComplexValueObjectAsComplexType)) ?? throw new Exception("Entity not found");
63+
var complexProperty = entityType.FindComplexProperty(nameof(TestEntityWithComplexValueObjectAsComplexType.TestComplexType)) ?? throw new Exception("Complex type property not found");
64+
65+
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexValueObject.TestEnum));
66+
}
5867
#endif
5968

6069
[Fact]

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/Extensions/ValueObjectDbContextOptionsBuilderExtensionsTests/UseValueObjectValueConverter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public void Should_add_converters_for_complex_types_inside_complex_value_object(
6464

6565
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
6666
}
67+
68+
[Fact]
69+
public void Should_add_converters_for_complex_value_object_as_complex_type()
70+
{
71+
var entityType = _ctx.Model.FindEntityType(typeof(TestEntityWithComplexValueObjectAsComplexType)) ?? throw new Exception("Entity not found");
72+
var complexProperty = entityType.FindComplexProperty(nameof(TestEntityWithComplexValueObjectAsComplexType.TestComplexType)) ?? throw new Exception("Complex type property not found");
73+
74+
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexValueObject.TestEnum));
75+
}
6776
#endif
6877

6978
[Fact]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Thinktecture.Runtime.Tests.TestEnums;
2+
3+
namespace Thinktecture.Runtime.Tests.TestEntities;
4+
5+
[ComplexValueObject]
6+
public partial class TestComplexValueObject
7+
{
8+
public TestEnum TestEnum { get; }
9+
}

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/TestEntities/TestDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class TestDbContext : DbContext
1313
#if COMPLEX_TYPES
1414
public DbSet<TestEntityWithComplexType> TestEntities_with_ComplexType { get; set; }
1515
public DbSet<ComplexValueObjectWithComplexType> ComplexValueObject_with_ComplexType { get; set; }
16+
public DbSet<TestEntityWithComplexValueObjectAsComplexType> TestEntities_with_ComplexValueObjectAsComplexType { get; set; }
1617
#endif
1718

1819
public TestDbContext(
@@ -42,6 +43,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4243
#if COMPLEX_TYPES
4344
TestEntityWithComplexType.Configure(modelBuilder, _valueConverterRegistration);
4445
ComplexValueObjectWithComplexType.Configure(modelBuilder, _valueConverterRegistration);
46+
TestEntityWithComplexValueObjectAsComplexType.Configure(modelBuilder, _valueConverterRegistration);
4547
#endif
4648

4749
if (_valueConverterRegistration == ValueConverterRegistration.OnModelCreating)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#if COMPLEX_TYPES
2+
using System;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace Thinktecture.Runtime.Tests.TestEntities;
6+
7+
public class TestEntityWithComplexValueObjectAsComplexType
8+
{
9+
public Guid Id { get; set; }
10+
public TestComplexValueObject TestComplexType { get; set; }
11+
12+
public static void Configure(
13+
ModelBuilder modelBuilder,
14+
ValueConverterRegistration valueConverterRegistration)
15+
{
16+
modelBuilder.Entity<TestEntityWithComplexValueObjectAsComplexType>(builder =>
17+
{
18+
builder.ComplexProperty(e => e.TestComplexType,
19+
b =>
20+
{
21+
b.IsRequired();
22+
b.Property(t => t.TestEnum);
23+
24+
if (valueConverterRegistration == ValueConverterRegistration.ComplexTypeConfiguration)
25+
b.AddValueObjectConverters(true);
26+
});
27+
28+
if (valueConverterRegistration == ValueConverterRegistration.EntityConfiguration)
29+
builder.AddValueObjectConverters(true);
30+
});
31+
}
32+
}
33+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#if COMPLEX_TYPES
2+
3+
namespace Thinktecture.Runtime.Tests.TestEntities;
4+
5+
public class Owned_with_ComplexType
6+
{
7+
public TestComplexType TestComplexType { get; set; }
8+
}
9+
10+
#endif

0 commit comments

Comments
 (0)