Skip to content

Commit 5298667

Browse files
Update design-time services documentation for EF Core 6.0+ (#5119)
Fixes #4013 Fixes #3390 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
1 parent f000667 commit 5298667

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

entity-framework/core/cli/services.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Design-time services - EF Core
33
description: Information on Entity Framework Core design-time services
44
author: SamMonoRT
5-
ms.date: 10/22/2020
5+
ms.date: 01/17/2025
66
uid: core/cli/services
77
---
88
# Design-time services
@@ -18,7 +18,7 @@ Microsoft.EntityFrameworkCore.Design is a DevelopmentDependency package. This me
1818
In order to reference its types and override design-time services, update the PackageReference item's metadata in your project file.
1919

2020
```xml
21-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.9">
21+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
2222
<PrivateAssets>all</PrivateAssets>
2323
<!-- Remove IncludeAssets to allow compiling against the assembly -->
2424
<!--<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>-->
@@ -34,19 +34,32 @@ The following is a list of the design-time services.
3434
Service | Description
3535
------------------------------------------------------------------------------------ | -----------
3636
<xref:Microsoft.EntityFrameworkCore.Design.IAnnotationCodeGenerator> | Generates the code for corresponding model annotations.
37+
<xref:Microsoft.EntityFrameworkCore.Design.ICandidateNamingService> | Generates candidate names for entities and properties.
3738
<xref:Microsoft.EntityFrameworkCore.Design.ICSharpHelper> | Helps with generating C# code.
39+
<xref:Microsoft.EntityFrameworkCore.Design.ICSharpMigrationOperationGenerator> | Generates C# code for migration operations.
40+
<xref:Microsoft.EntityFrameworkCore.Design.ICSharpSnapshotGenerator> | Generates C# code for model snapshots.
41+
<xref:Microsoft.EntityFrameworkCore.Design.ICSharpUtilities> | C# code generation utilities.
3842
<xref:Microsoft.EntityFrameworkCore.Design.IPluralizer> | Pluralizes and singularizes words.
3943
<xref:Microsoft.EntityFrameworkCore.Migrations.Design.IMigrationsCodeGenerator> | Generates code for a migration.
44+
<xref:Microsoft.EntityFrameworkCore.Migrations.Design.IMigrationsCodeGeneratorSelector> | Selects the appropriate migrations code generator.
4045
<xref:Microsoft.EntityFrameworkCore.Migrations.Design.IMigrationsScaffolder> | The main class for managing migration files.
46+
<xref:Microsoft.EntityFrameworkCore.Scaffolding.ICompiledModelCodeGenerator> | Generates code for compiled model metadata.
47+
<xref:Microsoft.EntityFrameworkCore.Scaffolding.ICompiledModelCodeGeneratorSelector> | Selects the appropriate compiled model code generator.
48+
<xref:Microsoft.EntityFrameworkCore.Scaffolding.ICompiledModelScaffolder> | The main class for scaffolding compiled models.
4149
<xref:Microsoft.EntityFrameworkCore.Scaffolding.IDatabaseModelFactory> | Creates a database model from a database.
4250
<xref:Microsoft.EntityFrameworkCore.Scaffolding.IModelCodeGenerator> | Generates code for a model.
51+
<xref:Microsoft.EntityFrameworkCore.Scaffolding.IModelCodeGeneratorSelector> | Selects the appropriate model code generator.
4352
<xref:Microsoft.EntityFrameworkCore.Scaffolding.IProviderConfigurationCodeGenerator> | Generates OnConfiguring code.
4453
<xref:Microsoft.EntityFrameworkCore.Scaffolding.IReverseEngineerScaffolder> | The main class for scaffolding reverse engineered models.
4554
<xref:Microsoft.EntityFrameworkCore.Scaffolding.IScaffoldingModelFactory> | Creates a model from a database model.
55+
<xref:Microsoft.EntityFrameworkCore.Scaffolding.IScaffoldingTypeMapper> | Maps database types to .NET types during scaffolding.
56+
<xref:Microsoft.EntityFrameworkCore.Scaffolding.ISnapshotModelProcessor> | Processes model snapshots.
57+
<xref:Microsoft.EntityFrameworkCore.Query.IPrecompiledQueryCodeGenerator> | Generates code for precompiled queries.
58+
<xref:Microsoft.EntityFrameworkCore.Query.IPrecompiledQueryCodeGeneratorSelector> | Selects the appropriate precompiled query code generator.
4659

4760
## Using services
4861

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

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

entity-framework/core/miscellaneous/internals/tools.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ In a nutshell, here are some of the strategies it uses.
114114

115115
### Design-time services
116116

117-
In addition to the application services and the internal DbContext services, there is a third set of [design-time services](xref:core/cli/services). These aren't added to internal service provider since they're never needed at runtime. The design-time services are built by [DesignTimeServicesBuilder](https://github.com/dotnet/efcore/blob/main/src/EFCore.Design/Design/Internal/DesignTimeServicesBuilder.cs). There are two main path--one with a context instance and one without. The one without is primarily used when scaffolding a new DbContext. There are several extensibility points here to allow the user, providers, and extensions to override and customize the services.
117+
In addition to the application services and the internal DbContext services, there is a third set of [design-time services](xref:core/cli/services). These aren't added to internal service provider since they're never needed at runtime. The design-time services are built by [DesignTimeServicesBuilder](https://github.com/dotnet/efcore/blob/main/src/EFCore.Design/Design/Internal/DesignTimeServicesBuilder.cs). There are two main paths--one with a context instance and one without. The one without is primarily used when scaffolding a new DbContext. There are several extensibility points here to allow the user, providers, and extensions to override and customize the services.
118118

119119
The user can customize services by adding an implementation of `IDesignTimeServices` to the startup assembly.
120120

entity-framework/core/performance/advanced-performance-topics.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ Because of these limitations, you should only use compiled models if your EF Cor
303303

304304
If supporting any of these features is critical to your success, then please vote for the appropriate issues linked above.
305305

306+
### Handling compilation errors due to ambiguous type references
307+
308+
When compiling models with types that have the same name but exist in different namespaces, the generated code may produce compilation errors due to ambiguous type references. To resolve this, you can customize the code generation to use fully-qualified type names by overriding `CSharpHelper.ShouldUseFullName` to return `true`. See [Design-time services](xref:core/cli/services) for information on how to override design-time services like `ICSharpHelper`.
309+
306310
## Reducing runtime overhead
307311

308312
As with any layer, EF Core adds a bit of runtime overhead compared to coding directly against lower-level database APIs. This runtime overhead is unlikely to impact most real-world applications in a significant way; the other topics in this performance guide, such as query efficiency, index usage and minimizing roundtrips, are far more important. In addition, even for highly-optimized applications, network latency and database I/O will usually dominate any time spent inside EF Core itself. However, for high-performance, low-latency applications where every bit of perf is important, the following recommendations can be used to reduce EF Core overhead to a minimum:
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
24
using Microsoft.EntityFrameworkCore;
35
using Microsoft.EntityFrameworkCore.Design;
6+
using Microsoft.EntityFrameworkCore.Infrastructure;
47
using Microsoft.EntityFrameworkCore.Migrations.Design;
8+
using Microsoft.EntityFrameworkCore.Storage;
59
using Microsoft.Extensions.DependencyInjection;
610

711
namespace CommandLine;
@@ -12,19 +16,27 @@ public static void AddMigration(string migrationName)
1216
{
1317
var projectDir = Directory.GetCurrentDirectory();
1418
var rootNamespace = "ConsoleApp1";
15-
var outputDir = "Migraitons";
19+
var outputDir = "Migrations";
1620

1721
#region CustomTools
18-
var db = new MyDbContext();
22+
using var db = new MyDbContext();
1923

2024
// Create design-time services
2125
var serviceCollection = new ServiceCollection();
22-
serviceCollection.AddEntityFrameworkDesignTimeServices();
2326
serviceCollection.AddDbContextDesignTimeServices(db);
27+
28+
var provider = db.GetService<IDatabaseProvider>().Name;
29+
var providerAssembly = Assembly.Load(new AssemblyName(provider));
30+
var providerServicesAttribute = providerAssembly.GetCustomAttribute<DesignTimeProviderServicesAttribute>();
31+
var designTimeServicesType = providerAssembly.GetType(providerServicesAttribute.TypeName, throwOnError: true);
32+
((IDesignTimeServices)Activator.CreateInstance(designTimeServicesType)!).ConfigureDesignTimeServices(serviceCollection);
33+
34+
serviceCollection.AddEntityFrameworkDesignTimeServices();
35+
2436
var serviceProvider = serviceCollection.BuildServiceProvider();
2537

2638
// Add a migration
27-
var migrationsScaffolder = serviceProvider.GetService<IMigrationsScaffolder>();
39+
var migrationsScaffolder = serviceProvider.GetRequiredService<IMigrationsScaffolder>();
2840
var migration = migrationsScaffolder.ScaffoldMigration(migrationName, rootNamespace);
2941
migrationsScaffolder.Save(projectDir, migration, outputDir);
3042
#endregion
@@ -33,4 +45,16 @@ public static void AddMigration(string migrationName)
3345

3446
internal class MyDbContext : DbContext
3547
{
48+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
49+
{
50+
optionsBuilder.UseSqlite(@"Data Source=test.db");
51+
}
52+
53+
public DbSet<Blog> Blogs { get; set; }
54+
}
55+
56+
internal class Blog
57+
{
58+
public int Id { get; set; }
59+
public string Title { get; set; }
3660
}

0 commit comments

Comments
 (0)