dotnet/efcore · error · InvalidOperationException

The model must be finalized and its runtime dependencies mus

Error message

The model must be finalized and its runtime dependencies must be initialized before '{method}' can be used. Ensure that either 'OnModelCreating' has completed or, if using a stand-alone 'ModelBuilder', that 'IModelRuntimeInitializer.Initialize(model.FinalizeModel())' was called.

What it means

GetRelationalDependencies (RelationalModelExtensions.cs:28) throws CoreStrings.ModelNotFinalized(methodName) when the model lacks the ModelDependencies runtime annotation. That annotation is stamped onto the model only during IModelRuntimeInitializer.Initialize (which finalizes the model and wires runtime services). Requesting relational dependencies earlier - on a not-yet-finalized or manually built IModel - means the runtime services are not present, so the API refuses rather than returning null.

Source

Thrown at src/EFCore.Relational/Infrastructure/RelationalModelExtensions.cs:28

/// </summary>
public static class RelationalModelExtensions
{
    /// <summary>
    ///     Returns the relational service dependencies.
    /// </summary>
    /// <remarks>
    ///     See <see href="https://aka.ms/efcore-docs-providers">Implementation of database providers and extensions</see>
    ///     for more information and examples.
    /// </remarks>
    /// <param name="model">The model.</param>
    /// <param name="methodName">The name of the calling method.</param>
    /// <returns>The relational service dependencies.</returns>
    public static RelationalModelDependencies GetRelationalDependencies(
        this IModel model,
        [CallerMemberName] string methodName = "")
        => (RelationalModelDependencies?)model
                .FindRuntimeAnnotation(RelationalAnnotationNames.ModelDependencies)?.Value
            ?? throw new InvalidOperationException(CoreStrings.ModelNotFinalized(methodName));
}

View on GitHub (pinned to dbf9771522)

Solutions

  1. Finalize and initialize the model: `var model = ((IModelRuntimeInitializer)serviceProvider.GetService(...)).Initialize modelBuilder.FinalizeModel()` or simply use a built DbContext whose OnModelCreating has completed.
  2. If using a stand-alone ModelBuilder, call IModelRuntimeInitializer.Initialize(modelBuilder.FinalizeModel()) before any runtime query.
  3. Defer the relational-dependency call until after the context has constructed its model (e.g. in a post-build hook rather than a convention).

Example fix

// before
var modelBuilder = new ModelBuilder();
modelBuilder.Entity<Blog>();
var deps = modelBuilder.Model.GetRelationalDependencies(); // throws - not finalized

// after
var model = modelBuilder.FinalizeModel();
var initializer = serviceProvider.GetRequiredService<IModelRuntimeInitializer>();
initializer.Initialize(model);
var deps = model.GetRelationalDependencies();
Defensive patterns

Strategy: validation

Validate before calling

if (model is not IRuntimeModel { IsReadonly: true } /* or check annotation */)
{
    // ensure finalized+initialized before calling GetRelationalDependencies
    var initializer = serviceProvider.GetRequiredService<IModelRuntimeInitializer>();
    model = initializer.Initialize(((IModel)model).FinalizeModel() is var f ? f : model);
}
var deps = model.GetRelationalDependencies();

Prevention

When it happens

Trigger: Calling a relational API that internally calls GetRelationalDependencies() on an IModel that came from a stand-alone ModelBuilder without FinalizeModel()+Initialize(), or before OnModelCreating completes, or from a convention running mid-build.

Common situations: Provider/tooling code building a model manually with new ModelBuilder() and then using it; querying model metadata inside OnModelCreating before the model is finalized; a custom convention that calls relational extensions too early; caching a non-finalized IModel and reusing it at runtime.

Related errors


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