Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 21, 2026

The dotnet ef dbcontext optimize command throws a NullReferenceException when using dotnet-ef tools ≥9.0 with EF Core 8.0 projects, despite successfully generating the compiled model.

Root Cause

EF Core 8.0's DbContextOperations.Optimize returns void, while 9.0+ returns IReadOnlyList<string>. When the 9.0+ tool invokes the 8.0 runtime via reflection, it receives null and attempts to enumerate it in ReportResults.

Changes

  • Add null check before calling ReportResults in DbContextOptimizeCommand.Execute
  • Preserves file path reporting for 9.0+ while maintaining backward compatibility with 8.0
var result = executor.OptimizeContext(...);

if (result != null)
{
    ReportResults(result);
}

This aligns with the tool's cross-version support pattern established in #37201.

Original prompt

This section details on the original issue you should resolve

<issue_title>NullReferenceException when running dotnet ef dbcontext optimize</issue_title>
<issue_description>### Bug description

Hi,
Whenever I run the optimize command on a scaffolded model I get the error:
dotnet ef dbcontext optimize -o Models --no-build
Successfully generated a compiled model, to use it call 'options.UseModel(NBWebSeed1ContextModel.Instance)'. Run this command again when the model is modified.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Tools.Commands.DbContextOptimizeCommand.ReportResults(IEnumerable`1 generatedFiles)
at Microsoft.EntityFrameworkCore.Tools.Commands.DbContextOptimizeCommand.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.b__0(String[] args)
at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
Object reference not set to an instance of an object.

Your code

dotnet ef dbcontext optimize -o Models --no-build
My tool version is 9.0.7 but I'm running the command against a model that targets .NET 8 and depends on Microsoft.EntityFrameworkCore.SqlServer 8.0.15

Stack traces


Verbose output


EF Core version

9.0.7

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 8.0

Operating system

Windows 10

IDE

Visual Studio 17.4.9</issue_description>

Comments on the Issue (you are @copilot in this section)

@AndriySvyryd Could you share a small standalone repro project? @AndriySvyryd Related to #35219

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

… null

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullReferenceException in dbcontext optimize command Fix NullReferenceException in dbcontext optimize with cross-version compatibility Jan 21, 2026
Copilot AI requested a review from AndriySvyryd January 21, 2026 05:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants