dotnet/efcore · error · InvalidOperationException

Metadata model returned should not be 'null'. Provider: {pro

Error message

Metadata model returned should not be 'null'. Provider: {providerTypeName}.

What it means

Thrown in ReverseEngineerScaffolder.ScaffoldModel (line 103, resource ProviderReturnedNullModel) when the ScaffoldingModelFactory returns a null IModel after reading the database model. The factory (_factory.Create(databaseModel, modelOptions)) is expected to always produce a model; null indicates the provider's scaffolding factory failed to translate the database model, which EF treats as a provider/tooling defect rather than a user config error.

Source

Thrown at src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs:103

        }
        else if (!codeOptions.SuppressOnConfiguring)
        {
            _reporter.WriteWarning(DesignStrings.SensitiveInformationWarning);
        }

        codeOptions.ConnectionString ??= connectionString;

        var databaseModel = _databaseModelFactory.Create(resolvedConnectionString, databaseOptions);
        var modelConnectionString = (string?)databaseModel[ScaffoldingAnnotationNames.ConnectionString];
        if (!string.IsNullOrEmpty(modelConnectionString))
        {
            codeOptions.ConnectionString = modelConnectionString;
        }

        var model = _factory.Create(databaseModel, modelOptions);
        if (model == null)
        {
            throw new InvalidOperationException(
                DesignStrings.ProviderReturnedNullModel(
                    _factory.GetType().ShortDisplayName()));
        }

        if (string.IsNullOrEmpty(codeOptions.ContextName))
        {
            var annotatedName = model.GetDatabaseName();
            codeOptions.ContextName = !string.IsNullOrEmpty(annotatedName)
                ? _code.Identifier(annotatedName + DbContextSuffix)
                : DefaultDbContextName;
        }

        var codeGenerator = ModelCodeGeneratorSelector.Select(codeOptions);

        return codeGenerator.GenerateModel(model, codeOptions);
    }

    /// <summary>

View on GitHub (pinned to dbf9771522)

Solutions

  1. Align versions of Microsoft.EntityFrameworkCore.Design and the database provider package (they must match the runtime major version).
  2. Adjust DatabaseModelFactoryOptions (tables/schemas filter) to exclude objects the provider cannot handle and isolate the trigger.
  3. Upgrade the provider to the latest patch; provider bugs causing null models are often fixed.
  4. If you supply a custom ScaffoldingModelFactory, ensure Create never returns null; return an empty model or throw a meaningful exception instead.
Defensive patterns

Strategy: validation

Validate before calling

// After creating the database model, guard the factory result
var model = factory.Create(databaseModel, modelOptions);
if (model is null)
    throw new InvalidOperationException($"Provider {factory.GetType()} returned a null model.");

Try / catch

try { scaffolder.ScaffoldModel(...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Metadata model returned should not be null"))
{ /* align Design + provider versions, narrow table selection, upgrade provider, then retry */ }

Prevention

When it happens

Trigger: After _databaseModelFactory.Create succeeds, _factory.Create(databaseModel, modelOptions) returns null. The `if (model == null)` guard throws ProviderReturnedNullModel(factory.GetType().ShortDisplayName()). The provider's ScaffoldingModelFactory bailed without producing a model.

Common situations: A database the provider cannot reverse-engineer (unsupported schema constructs, exotic types, empty/no tables selected). A version mismatch between the EF Core Design package and the provider package causing the factory to return null. A misconfigured/custom ScaffoldingModelFactory. Provider bugs for specific DB objects.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/ac701f7f849ab274. Report an issue: GitHub.