Skip to content

Conversation

Copy link

Copilot AI commented Nov 23, 2025

When a class explicitly implements an interface property (e.g., string IStringId.Id => Id.ToString()) while also having a property with the same name (public int Id), GetImplementingProperty throws InvalidOperationException: Sequence contains no matching element.

Changes

  • TypeExtensions.cs: Replace .First() with .FirstOrDefault() ?? propertyInfo to gracefully handle cases where the implementing property isn't found in the derived type's property list
  • TypeExtensionTests.cs: Add unit test coverage for explicit interface implementation scenarios
  • ExplicitInterfaceImplementationTests.cs: Add functional tests to validate the fix works correctly with EF Core queries (Select and Where operations)
  • ProjectionExpressionGeneratorTests.cs: Add generator test to verify source generation handles explicit interface implementations correctly

Example

public interface IStringId { string Id { get; } }

public class Item : IStringId
{
    public int Id { get; set; }
    string IStringId.Id => Id.ToString();  // Explicit implementation
}

// Previously threw InvalidOperationException, now works correctly
var property = typeof(Item).GetImplementingProperty(typeof(IStringId).GetProperty("Id"));

Testing

  • ✅ All 161 tests pass (24 unit tests, 67 functional tests, 71 generator tests)
  • ✅ Unit tests verify the fix at the TypeExtensions level
  • ✅ Functional tests validate the fix works in real EF Core query scenarios
  • ✅ Generator test confirms source generation works correctly with explicit interface implementations
Original prompt

Fix this issue :

Two properties with same name causes GetImplementingProperty to throw System.InvalidOperationException: Sequence contains no matching element

Simplification of my setup:

public interface IStringId { string Id { get; } }

public class Item
{
public int Id { get; set ; }
string IStringId.Id => Id.ToString();
}

public Task GetItem(string id) => db.Items.FirstOrDefaultAsync(x => x.Id == id);

GetItem method throws InvalidOperationException when UseProjectables() is called, but works fine otherwise.

I see that TypeExtensions.GetConcreteProperty is called with propertyInfo being the string Id property from IStringId

DeclaringType is an interface, so it calls GetImplementingProperty, but it's looking for a property that isn't declared on this class, so this .First throws the InvalidOperationException:

        return derivedProperties.First(propertyInfo.GetMethod == accessor
            ? p => p.GetMethod == implementingAccessor
            : p => p.SetMethod == implementingAccessor);

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix InvalidOperationException with GetImplementingProperty Fix InvalidOperationException in GetImplementingProperty with explicit interface implementations Nov 23, 2025
Copilot AI requested a review from PhenX November 23, 2025 10:05
@PhenX
Copy link
Owner

PhenX commented Nov 23, 2025

@copilot Please also add functional and generator tests in there respective projects

Copy link
Author

Copilot AI commented Nov 23, 2025

@copilot Please also add functional and generator tests in there respective projects

Added functional and generator tests as requested in commit 201cabe.

Functional tests (ExplicitInterfaceImplementationTests.cs):

  • ProjectOverExplicitInterfaceImplementation - Tests Select queries
  • FilterOnExplicitInterfaceImplementation - Tests Where queries

Generator test (ProjectionExpressionGeneratorTests.cs):

  • ExplicitInterfaceImplementation - Verifies source generation for explicit interface implementations

All 161 tests pass (24 unit tests, 67 functional tests, 71 generator tests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants