Skip to content

Add EF Core 9.0 Auto-Compiled Models Support #525

@danielmackay

Description

@danielmackay

Summary

Implement EF Core 9.0 auto-compiled models feature to improve application startup performance by automatically detecting and using compiled models without requiring manual configuration.

Background

EF Core 9.0 introduces auto-compiled models that can significantly improve startup time for applications with large models (100s or 1000s of entity types). This feature eliminates the need to manually configure compiled models and automatically detects when a compiled model is available.

Key Benefits:

  • Faster startup time - Heavy lifting of processing and compiling LINQ queries into SQL no longer happens at application startup
  • Automatic detection - No need to manually call .UseModel() when the compiled model is in the same project/assembly
  • MSBuild integration - Can automatically update compiled models when the model project is built
  • Improved performance - Each query's interceptor contains finalized SQL and optimized materialization code

Technical Details

What's New in EF Core 9.0:

  1. Auto-detection: Compiled models are automatically detected and used when in the same project/assembly as the DbContext
  2. MSBuild integration: Can automatically regenerate compiled models during build process
  3. Simplified workflow: No longer requires manual .UseModel(MyCompiledModels.BlogsContextModel.Instance) calls

Before (EF Core 8.x):

// Manual command required
dotnet ef dbcontext optimize

// Manual configuration required
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder.UseModel(MyCompiledModels.BlogsContextModel.Instance);

After (EF Core 9.0):

// Automatic detection - no manual configuration needed
// Just run: dotnet ef dbcontext optimize
// Or use MSBuild integration for automatic builds

Acceptance Criteria

Phase 1: Basic Auto-Compiled Models

  • Upgrade to EF Core 9.0 in the template
  • Test auto-compiled models work without manual .UseModel() configuration
  • Verify startup performance improvements with compiled models
  • Update documentation with EF Core 9.0 compiled models guidance

Phase 2: MSBuild Integration (Optional)

  • Add Microsoft.EntityFrameworkCore.Tasks NuGet package
  • Configure MSBuild properties for automatic model compilation:
    <PropertyGroup>
      <EFOptimizeContext>true</EFOptimizeContext>
      <EFScaffoldModelStage>build</EFScaffoldModelStage>
    </PropertyGroup>
  • Test that compiled models are automatically regenerated on build
  • Document the MSBuild integration approach

Phase 3: Documentation & Guidelines

  • Add section to documentation about EF Core 9.0 compiled models
  • Include performance comparison benchmarks (with/without compiled models)
  • Provide guidance on when to use compiled models (large models with 100+ entities)
  • Add troubleshooting section for compiled model issues

Implementation Notes

Performance Considerations:

  • Most beneficial for applications with large models (100s or 1000s of entity types)
  • Reduces startup time by pre-compiling LINQ query processing
  • Generated interceptors contain optimized SQL and materialization code
  • Uses unsafe accessors for efficient object materialization

Compatibility:

  • Requires EF Core 9.0+
  • Works with .NET 8+ (EF 9 targets .NET 8)
  • Auto-detection only works when DbContext and compiled model are in same assembly

Example Generated Code:

The feature generates interceptors with embedded SQL and materialization code:

// Pre-compiled SQL
var relationalCommandTemplate = new RelationalCommand(
    materializerLiftableConstantContext.CommandBuilderDependencies, 
    "SELECT [b].[Id], [b].[Name]\nFROM [Blogs] AS [b]\nWHERE [b].[Name] = N'foo'", 
    new IRelationalParameter[] { });

// Optimized materialization using unsafe accessors
var instance = new Blog();
UnsafeAccessor_Blog_Id_Set(instance) = dataReader.GetInt32(0);
UnsafeAccessor_Blog_Name_Set(instance) = dataReader.GetString(1);

Resources

Priority

Medium - Performance enhancement that provides measurable startup improvements for applications with large EF models.

Labels

  • enhancement
  • ef-core
  • performance
  • dotnet-9

This issue implements the EF Core 9.0 auto-compiled models feature as described in the official Microsoft documentation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions