Skip to content

[release/10.0] Fix NullReferenceException in dbcontext optimize with cross-version compatibility#37547

Open
Copilot wants to merge 2 commits intorelease/10.0from
copilot/fix-null-reference-exception
Open

[release/10.0] Fix NullReferenceException in dbcontext optimize with cross-version compatibility#37547
Copilot wants to merge 2 commits intorelease/10.0from
copilot/fix-null-reference-exception

Conversation

Copy link
Contributor

Copilot AI commented Jan 21, 2026

Fixes #36437

Description

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

Customer impact

When customers use dotnet-ef tools 9.0+ with EF Core 8.0 projects and run dotnet ef dbcontext optimize, they see a NullReferenceException error message after the compiled model is successfully generated. The error appears at DbContextOptimizeCommand.ReportResults(IEnumerable<string> generatedFiles) when attempting to iterate over a null result.

While the compiled model files are still created correctly, the error message can be confusing and may cause build pipeline failures if not handled properly.

Workaround: Customers can either:

  1. Use dotnet-ef tools 8.0.22 or earlier with EF Core 8.0 projects, or
  2. Upgrade to EF Core 9.0+ to match their dotnet-ef tools version

How found

Multiple customer reports on EF 9.0.x and 10.0.x

Regression

Yes, introduced in #33028 (part of #24894). The issue occurs because:

Testing

Verified manually

Risk

Low. The change is minimal (3-line addition).

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
@AndriySvyryd AndriySvyryd changed the title Fix NullReferenceException in dbcontext optimize with cross-version compatibility [release/10.0] Fix NullReferenceException in dbcontext optimize with cross-version compatibility Feb 13, 2026
@AndriySvyryd
Copy link
Member

@copilot Update progress by rewriting the PR summary using this template:

Fixes #{number}

**Description**
{Issue description}

**Customer impact**
{How does the reported issue affects customer's app? Are there workarounds?}

**How found**
{Was it customer reported or found during verification? How many customers are affected?}

**Regression**
{Is it a regression from a released version? Which one?}

**Testing**
{How the changes were tested}

**Risk**
{Low/Medium/High, argument why}

Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot Update progress by rewriting the PR summary using this template:

...

Updated the PR description with the structured template.

@AndriySvyryd AndriySvyryd marked this pull request as ready for review February 13, 2026 04:32
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner February 13, 2026 04:32
Copilot AI review requested due to automatic review settings February 13, 2026 04:32
@AndriySvyryd AndriySvyryd requested a review from artl93 February 13, 2026 04:41
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a NullReferenceException in the dotnet ef dbcontext optimize command when using dotnet-ef tools version 9.0+ with EF Core 8.0 projects. The issue occurs because DbContextOperations.Optimize changed its return type from void (8.0) to IReadOnlyList<string> (9.0+), causing the tools to receive a null result when operating on EF Core 8.0 projects. The fix adds a null check before attempting to report results.

Changes:

  • Added null check before calling ReportResults in DbContextOptimizeCommand.Execute to handle cross-version compatibility

Copy link
Member

@artl93 artl93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression, customer reported, non-obvious workaround, approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants