Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 19, 2025

Fixes #34617

Description
The EF Core design-time services discovery mechanism was attempting to instantiate abstract classes implementing IDesignTimeServices, which resulted in a MissingMethodException when running commands like dotnet ef migrations add.

Customer impact
All types implemented IDesignTimeServices have to be made instantiable as a work-around.

How found
Customer reported on 8.0.8

Regression
No

Testing
Tests added

Risk
Low. The fix is very localized.

Example scenario that failed before this fix:

[assembly: DesignTimeServicesReference("Data.Entity.ActualDesignTimeServices, MyProject")]

public abstract class DesignTimeServicesBase : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        DoSomething();
    }

    protected abstract void DoSomething();
}

public class ActualDesignTimeServices : DesignTimeServicesBase
{
    protected override void DoSomething()
    {
        // Implementation
    }
}

Running dotnet ef migrations add would throw:

System.MissingMethodException: Cannot dynamically create an instance of type 'DesignTimeServicesBase'. 
Reason: Cannot create an abstract class.

Fix

Modified the type discovery filter to use IsInstantiable() instead of checking only !IsAbstract:

.Where(t => typeof(IDesignTimeServices).IsAssignableFrom(t) && t.IsInstantiable())

The IsInstantiable() extension method provides comprehensive filtering by excluding:

  • Abstract classes (which cannot be instantiated)
  • Interfaces (which cannot be instantiated)
  • Generic type definitions (which cannot be instantiated)

This change:

  • Aligns with EF Core's existing pattern for finding constructible types in GetConstructibleTypes()
  • Only affects the automatic discovery mechanism - explicit type references via DesignTimeServicesReferenceAttribute are unchanged
  • Provides more robust type filtering than just checking !IsAbstract

Testing

Added comprehensive tests covering:

  • Abstract base classes with concrete implementations
  • Interfaces implementing IDesignTimeServices
  • Mixed scenarios with both abstract and concrete types
  • Verification that concrete types are properly selected

All existing tests continue to pass, confirming no regressions in the design-time services functionality.

@Copilot Copilot AI changed the title [WIP] Abstract IDesignTimeServices leads to MissingMethodException with ef migrations add Fix MissingMethodException when abstract IDesignTimeServices classes are discovered Aug 19, 2025
@Copilot Copilot AI requested a review from AndriySvyryd August 19, 2025 17:41
Copilot finished work on behalf of AndriySvyryd August 19, 2025 17:41
Copilot finished work on behalf of AndriySvyryd August 19, 2025 17:51
@AndriySvyryd AndriySvyryd marked this pull request as ready for review August 19, 2025 18:43
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner August 19, 2025 18:43
@AndriySvyryd AndriySvyryd changed the title Fix MissingMethodException when abstract IDesignTimeServices classes are discovered [rc2] Fix MissingMethodException when abstract IDesignTimeServices classes are discovered Aug 19, 2025
@AndriySvyryd AndriySvyryd requested a review from artl93 August 19, 2025 19:22
@AndriySvyryd AndriySvyryd merged commit 7605c0d into release/10.0 Aug 25, 2025
7 checks passed
@AndriySvyryd AndriySvyryd deleted the copilot/fix-34617 branch August 25, 2025 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Abstract IDesignTimeServices leads to MissingMethodException with ef migrations add

4 participants