Design-time services documentation out of date
[Enter feedback here]
This documentation is out of date.
In net 6.0 this line var migrationsScaffolder = serviceProvider.GetService<IMigrationsScaffolder>() throws error.
See https://github.com/dotnet/efcore/issues/23595
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 2886eabf-4b28-c390-9634-14e623e8ec8e
- Version Independent ID: 1ef0f90d-664c-81f7-ee5d-f67dafce4ed1
- Content: Design-time services - EF Core
- Content Source: entity-framework/core/cli/services.md
- Product: entity-framework
- Technology: entity-framework-core
- GitHub Login: @bricelam
- Microsoft Alias: avickers
I ran into this same issue when trying to use https://github.com/dotnet/efcore/issues/23595 with
<TargetFramework>net6.0</TargetFramework>
and
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.6">
I was able to get it working by adding the following registrations before calling BuildServiceProvider:
var serviceCollection = new ServiceCollection();
serviceCollection.AddEntityFrameworkDesignTimeServices();
serviceCollection.AddDbContextDesignTimeServices(context);
// these additional registrations were needed on dotnet 6.0
serviceCollection.AddScoped<AnnotationCodeGeneratorDependencies>();
serviceCollection.AddScoped<TypeMappingSourceDependencies>();
serviceCollection.AddScoped<ValueConverterSelectorDependencies>();
serviceCollection.AddScoped<RelationalTypeMappingSourceDependencies>();
serviceCollection.AddSingleton<IValueConverterSelector, ValueConverterSelector>();
serviceCollection.AddSingleton<ITypeMappingSource, SqlServerTypeMappingSource>();
serviceCollection.AddSingleton<IAnnotationCodeGenerator, SqlServerAnnotationCodeGenerator>();
var serviceProvider = serviceCollection.BuildServiceProvider();
var migrationsScaffolder = serviceProvider.GetRequiredService<IMigrationsScaffolder>();
@AndriySvyryd Did these APIs break? Do they need to be called in a different order? Is an additional call needed on the service collectoin?
@wiggins-twentyideas there is another way see https://github.com/dotnet/efcore/issues/23595#issuecomment-966185003
@bricelam yes API did break. In net5.0 configuring design time service was not required.
Now we don't add redundant relational-level services. Provider services always need to be added as they might have different implementation.
var provider = context.GetService<IDatabaseProvider>().Name;
var providerAssembly = Assembly.Load(new AssemblyName(provider));
var providerServicesAttribute = providerAssembly.GetCustomAttribute<DesignTimeProviderServicesAttribute>();
var designTimeServicesType = providerAssembly.GetType(providerServicesAttribute.TypeName, throwOnError: true);
((IDesignTimeServices)Activator.CreateInstance(designTimeServicesType)!).ConfigureDesignTimeServices(serviceCollection);
Previously, this line was responsible for resolving the provider-specific services:
serviceCollection.AddDbContextDesignTimeServices(context);
See also https://github.com/dotnet/efcore/issues/27502