Back to Entityframework

Design-time services - EF Core

entity-framework/core/cli/services.md

latest4.5 KB
Original Source

Design-time services

Some services used by the tools are only used at design time. These services are managed separately from EF Core's runtime services to prevent them from being deployed with your app. To override one of these services (for example the service to generate migration files), add an implementation of IDesignTimeServices to your startup project.

[!code-csharpMain]

Referencing Microsoft.EntityFrameworkCore.Design

Microsoft.EntityFrameworkCore.Design is a DevelopmentDependency package. This means that the dependency won't flow transitively into other projects, and that you cannot, by default, reference its types.

In order to reference its types and override design-time services, update the PackageReference item's metadata in your project file.

xml
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
  <PrivateAssets>all</PrivateAssets>
  <!-- Remove IncludeAssets to allow compiling against the assembly -->
  <!--<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>-->
</PackageReference>

If the package is being referenced transitively via Microsoft.EntityFrameworkCore.Tools, you will need to add an explicit PackageReference to the package and change its metadata.

List of services

The following is a list of the design-time services.

ServiceDescription
xref:Microsoft.EntityFrameworkCore.Design.IAnnotationCodeGeneratorGenerates the code for corresponding model annotations.
xref:Microsoft.EntityFrameworkCore.Design.ICSharpHelperHelps with generating C# code.
xref:Microsoft.EntityFrameworkCore.Design.IPluralizerPluralizes and singularizes words.
xref:Microsoft.EntityFrameworkCore.Migrations.Design.ICSharpMigrationOperationGeneratorGenerates C# code for migration operations.
xref:Microsoft.EntityFrameworkCore.Migrations.Design.ICSharpSnapshotGeneratorGenerates C# code for model snapshots.
xref:Microsoft.EntityFrameworkCore.Migrations.Design.IMigrationsCodeGeneratorGenerates code for a migration.
xref:Microsoft.EntityFrameworkCore.Migrations.Design.IMigrationsCodeGeneratorSelectorSelects the appropriate migrations code generator.
xref:Microsoft.EntityFrameworkCore.Migrations.Design.IMigrationsScaffolderThe main class for managing migration files.
xref:Microsoft.EntityFrameworkCore.Scaffolding.ICompiledModelCodeGeneratorGenerates code for compiled model metadata.
xref:Microsoft.EntityFrameworkCore.Scaffolding.ICompiledModelCodeGeneratorSelectorSelects the appropriate compiled model code generator.
xref:Microsoft.EntityFrameworkCore.Scaffolding.ICompiledModelScaffolderThe main class for scaffolding compiled models.
xref:Microsoft.EntityFrameworkCore.Scaffolding.IDatabaseModelFactoryCreates a database model from a database.
xref:Microsoft.EntityFrameworkCore.Scaffolding.IModelCodeGeneratorGenerates code for a model.
xref:Microsoft.EntityFrameworkCore.Scaffolding.IModelCodeGeneratorSelectorSelects the appropriate model code generator.
xref:Microsoft.EntityFrameworkCore.Scaffolding.IProviderConfigurationCodeGeneratorGenerates OnConfiguring code.
xref:Microsoft.EntityFrameworkCore.Scaffolding.IReverseEngineerScaffolderThe main class for scaffolding reverse engineered models.
xref:Microsoft.EntityFrameworkCore.Scaffolding.IScaffoldingModelFactoryCreates a model from a database model.
xref:Microsoft.EntityFrameworkCore.Query.IPrecompiledQueryCodeGeneratorGenerates code for precompiled queries.
xref:Microsoft.EntityFrameworkCore.Query.IPrecompiledQueryCodeGeneratorSelectorSelects the appropriate precompiled query code generator.

Using services

These services can also be useful for creating your own tools. For example, when you want to automate part of your design-time workflow.

You can build a service provider containing these services using the AddEntityFrameworkDesignTimeServices and AddDbContextDesignTimeServices extension methods.

[!code-csharp]