diff --git a/entity-framework/core/cli/services.md b/entity-framework/core/cli/services.md index 985392c98b..73926d4e7b 100644 --- a/entity-framework/core/cli/services.md +++ b/entity-framework/core/cli/services.md @@ -2,7 +2,7 @@ title: Design-time services - EF Core description: Information on Entity Framework Core design-time services author: SamMonoRT -ms.date: 10/22/2020 +ms.date: 01/17/2025 uid: core/cli/services --- # Design-time services @@ -18,7 +18,7 @@ Microsoft.EntityFrameworkCore.Design is a DevelopmentDependency package. This me In order to reference its types and override design-time services, update the PackageReference item's metadata in your project file. ```xml - + all @@ -34,19 +34,32 @@ The following is a list of the design-time services. Service | Description ------------------------------------------------------------------------------------ | ----------- | Generates the code for corresponding model annotations. + | Generates candidate names for entities and properties. | Helps with generating C# code. + | Generates C# code for migration operations. + | Generates C# code for model snapshots. + | C# code generation utilities. | Pluralizes and singularizes words. | Generates code for a migration. + | Selects the appropriate migrations code generator. | The main class for managing migration files. + | Generates code for compiled model metadata. + | Selects the appropriate compiled model code generator. + | The main class for scaffolding compiled models. | Creates a database model from a database. | Generates code for a model. + | Selects the appropriate model code generator. | Generates OnConfiguring code. | The main class for scaffolding reverse engineered models. | Creates a model from a database model. + | Maps database types to .NET types during scaffolding. + | Processes model snapshots. + | Generates code for precompiled queries. + | Selects 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 you design-time workflow. +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. diff --git a/entity-framework/core/miscellaneous/internals/tools.md b/entity-framework/core/miscellaneous/internals/tools.md index 92b213790d..56b7dc40b0 100644 --- a/entity-framework/core/miscellaneous/internals/tools.md +++ b/entity-framework/core/miscellaneous/internals/tools.md @@ -114,7 +114,7 @@ In a nutshell, here are some of the strategies it uses. ### Design-time services -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. +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. The user can customize services by adding an implementation of `IDesignTimeServices` to the startup assembly. diff --git a/entity-framework/core/performance/advanced-performance-topics.md b/entity-framework/core/performance/advanced-performance-topics.md index bcd662ae1f..b081f31570 100644 --- a/entity-framework/core/performance/advanced-performance-topics.md +++ b/entity-framework/core/performance/advanced-performance-topics.md @@ -302,6 +302,10 @@ Because of these limitations, you should only use compiled models if your EF Cor If supporting any of these features is critical to your success, then please vote for the appropriate issues linked above. +### Handling compilation errors due to ambiguous type references + +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`. + ## Reducing runtime overhead 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: diff --git a/samples/core/Miscellaneous/CommandLine/CustomTools.cs b/samples/core/Miscellaneous/CommandLine/CustomTools.cs index d4e20448a1..8450b1c463 100644 --- a/samples/core/Miscellaneous/CommandLine/CustomTools.cs +++ b/samples/core/Miscellaneous/CommandLine/CustomTools.cs @@ -1,7 +1,11 @@ -using System.IO; +using System; +using System.IO; +using System.Reflection; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations.Design; +using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; namespace CommandLine; @@ -12,19 +16,27 @@ public static void AddMigration(string migrationName) { var projectDir = Directory.GetCurrentDirectory(); var rootNamespace = "ConsoleApp1"; - var outputDir = "Migraitons"; + var outputDir = "Migrations"; #region CustomTools - var db = new MyDbContext(); + using var db = new MyDbContext(); // Create design-time services var serviceCollection = new ServiceCollection(); - serviceCollection.AddEntityFrameworkDesignTimeServices(); serviceCollection.AddDbContextDesignTimeServices(db); + + var provider = db.GetService().Name; + var providerAssembly = Assembly.Load(new AssemblyName(provider)); + var providerServicesAttribute = providerAssembly.GetCustomAttribute(); + var designTimeServicesType = providerAssembly.GetType(providerServicesAttribute.TypeName, throwOnError: true); + ((IDesignTimeServices)Activator.CreateInstance(designTimeServicesType)!).ConfigureDesignTimeServices(serviceCollection); + + serviceCollection.AddEntityFrameworkDesignTimeServices(); + var serviceProvider = serviceCollection.BuildServiceProvider(); // Add a migration - var migrationsScaffolder = serviceProvider.GetService(); + var migrationsScaffolder = serviceProvider.GetRequiredService(); var migration = migrationsScaffolder.ScaffoldMigration(migrationName, rootNamespace); migrationsScaffolder.Save(projectDir, migration, outputDir); #endregion @@ -33,4 +45,16 @@ public static void AddMigration(string migrationName) internal class MyDbContext : DbContext { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite(@"Data Source=test.db"); + } + + public DbSet Blogs { get; set; } +} + +internal class Blog +{ + public int Id { get; set; } + public string Title { get; set; } } \ No newline at end of file