{"record":{"id":"27389e64ff45f536","repo":"dotnet/efcore","slug":"the-model-must-be-finalized-and-its-runtime-depend-27389e","errorCode":null,"errorMessage":"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.","messagePattern":"The model must be finalized and its runtime dependencies must be initialized before '(.+?)' can be used\\. Ensure that either 'OnModelCreating' has completed or, if using a stand-alone 'ModelBuilder', that 'IModelRuntimeInitializer\\.Initialize\\(model\\.FinalizeModel\\(\\)\\)' was called\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelExtensions.cs","lineNumber":28,"sourceCode":"/// </summary>\npublic static class RelationalModelExtensions\n{\n    /// <summary>\n    ///     Returns the relational service dependencies.\n    /// </summary>\n    /// <remarks>\n    ///     See <see href=\"https://aka.ms/efcore-docs-providers\">Implementation of database providers and extensions</see>\n    ///     for more information and examples.\n    /// </remarks>\n    /// <param name=\"model\">The model.</param>\n    /// <param name=\"methodName\">The name of the calling method.</param>\n    /// <returns>The relational service dependencies.</returns>\n    public static RelationalModelDependencies GetRelationalDependencies(\n        this IModel model,\n        [CallerMemberName] string methodName = \"\")\n        => (RelationalModelDependencies?)model\n                .FindRuntimeAnnotation(RelationalAnnotationNames.ModelDependencies)?.Value\n            ?? throw new InvalidOperationException(CoreStrings.ModelNotFinalized(methodName));\n}\n","sourceCodeStart":10,"sourceCodeEnd":30,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelExtensions.cs#L10-L30","documentation":"GetRelationalDependencies throws ModelNotFinalized when the model lacks the ModelDependencies runtime annotation, meaning it was never run through IModelRuntimeInitializer.Initialize(model.FinalizeModel()). Relational dependencies are attached only at runtime-model finalization; calling relational runtime APIs on a partially built IModel is a programming error.","triggerScenarios":"Constructing a ModelBuilder standalone (not via DbContext.OnModelCreating) and immediately calling a relational extension that needs RelationalModelDependencies — e.g. GetRelationalDependencies, runtime annotation accessors that depend on it. The [CallerMemberName] is injected into the message so the offending method is named.","commonSituations":"Provider authors writing tests against an unfinalized convention model; tooling that reads the model before it is published; calling relational APIs on model.GetModel() from a convention without running finalization; OnModelCreating throwing before completion leaving a half-built model.","solutions":["Run IModelRuntimeInitializer.Initialize(model.FinalizeModel()) before calling the method named in the error.","Inside a DbContext, ensure OnModelCreating returns without exception; the runtime initializer runs after OnModelCreating.","In tests, obtain the finalized model via the DbContext (DbContext.Model) rather than a raw ModelBuilder product."],"exampleFix":"// before\nvar mb = new ModelBuilder(new ConventionSet());\nmb.Entity<Blog>();\nvar deps = mb.Model.GetRelationalDependencies(); // throws\n\n// after\nvar finalized = mb.FinalizeModel();\nvar initializer = new RuntimeModelInitializer(/* services */);\ninitializer.Initialize(finalized);\nvar deps = finalized.GetRelationalDependencies();","handlingStrategy":"validation","validationCode":"var initialized = runtimeInitializer.Initialize(model.FinalizeModel());\nvar deps = initialized.GetRelationalDependencies();","typeGuard":"static bool IsFinalized(IModel m)\n    => m.FindRuntimeAnnotation(RelationalAnnotationNames.ModelDependencies) is not null;","tryCatchPattern":null,"preventionTips":["Always call FinalizeModel + IModelRuntimeInitializer.Initialize on standalone ModelBuilder output.","Obtain models from DbContext.Model in tests to get an already-finalized runtime model.","Ensure OnModelCreating does not throw, which would leave the model unfinalized."],"tags":["model-finalization","runtime-initializer","provider","design-time"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}