Skip to content

"Design-time services/Using services" sample throws InvalidOperationException #4454

@a2er

Description

@a2er

Specific Case

The sample to request design time services throws an InvalidOperationException (when executed with .NET 6 / ef7):

Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Design.IAnnotationCodeGenerator' while attempting to activate 'Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsCodeGeneratorDependencies'.

General Case

The sample to request design time services does not work for all listed services in section List of services.

According my tests the sample works only for a subset of services:

  • .NET 5: IAnnotationCodeGenerator, ICSharpHelper, IPluralizer, IMigrationsCodeGenerator, IMigrationsScaffolder
  • .NET 6-7: IPluralizer and IScaffoldingModelFactory
  • .NET 8: IModelCodeGenerator, IPluralizer, IScaffoldingModelFactory

Other services will return null or throw an exception due to unresolved dependencies.

Comparing the example with DesignTimeServicesBuilder.Build I assume that this is expected behaviour since the provider design-time services are not loaded.


Long story short

The section Using services should mention which services can be used effectively or the public API should get the capabilities to load provider, referenced and implementation design-time services equal to DesignTimeServicesBuilder.Build.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions