Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit b7c5449

Browse files
committed
Fix for #179. Ambiguous exception when working with properties.
1 parent 109e613 commit b7c5449

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,23 @@ public override int GetHashCode()
495495
return found;
496496
}
497497

498+
private static PropertyInfo GetPropertyInfoForType(Type type, PropertyInfo propInfo)
499+
{
500+
if (propInfo.DeclaringType == type)
501+
return propInfo;
502+
503+
var found = type.GetProperties()
504+
.FirstOrDefault(x => x.Module == propInfo.Module && x.MetadataToken == propInfo.MetadataToken);
505+
506+
return found ?? propInfo;
507+
}
508+
498509
private Sql.ExpressionAttribute? GetDbFunctionFromProperty(Type type, PropertyInfo propInfo)
499510
{
500511
if (_dependencies == null || _model == null)
501512
return null;
502513

503-
propInfo = (PropertyInfo?) type.GetMemberEx(propInfo) ?? propInfo;
514+
propInfo = GetPropertyInfoForType(type, propInfo);
504515

505516
var found = _calculatedExtensions.GetOrAdd(propInfo, mi =>
506517
{

Tests/LinqToDB.EntityFrameworkCore.BaseTests/ForMappingTestsBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,14 @@ public virtual async Task TestUIntTable()
108108
var item = await context.UIntTable.FirstOrDefaultAsyncLinqToDB(e => e.Field64 == field64);
109109
}
110110

111+
[Test]
112+
public virtual void TestAmbiguousProperties()
113+
{
114+
using var context = CreateContext();
115+
116+
FluentActions.Awaiting(() => context.WithDuplicateProperties.Where(x => x.Value == 1)
117+
.ToArrayAsyncLinqToDB()).Should().NotThrow();
118+
}
119+
111120
}
112121
}

Tests/LinqToDB.EntityFrameworkCore.BaseTests/Models/ForMapping/ForMappingContextBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ protected ForMappingContextBase(DbContextOptions options) : base(options)
1212
public DbSet<NoIdentity> NoIdentity { get; set; } = null!;
1313
public DbSet<UIntTable> UIntTable { get; set; } = null!;
1414
public DbSet<StringTypes> StringTypes { get; set; } = null!;
15+
16+
public DbSet<WithDuplicateProperties> WithDuplicateProperties { get; set; } = null!;
1517
}
1618
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
4+
{
5+
6+
public class WithDuplicatePropertiesBase
7+
{
8+
[Key]
9+
public int Id { get; set; }
10+
11+
public virtual string? Value { get; set; }
12+
}
13+
14+
public class WithDuplicateProperties : WithDuplicatePropertiesBase
15+
{
16+
public new int? Value { get; set; }
17+
}
18+
}

0 commit comments

Comments
 (0)