{"record":{"id":"060f88b8772a294a","repo":"dotnet/efcore","slug":"the-requested-configuration-is-not-stored-in-the-r","errorCode":null,"errorMessage":"The requested configuration is not stored in the read-optimized model, please use 'DbContext.GetService<IDesignTimeModel>().Model'.","messagePattern":"The requested configuration is not stored in the read-optimized model, please use 'DbContext\\.GetService<IDesignTimeModel>\\(\\)\\.Model'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Extensions/CosmosIndexExtensions.cs","lineNumber":26,"sourceCode":"\n/// <summary>\n///     Index extension methods for Azure Cosmos DB-specific metadata.\n/// </summary>\n/// <remarks>\n///     See <see href=\"https://aka.ms/efcore-docs-modeling\">Modeling entity types and relationships</see>, and\n///     <see href=\"https://aka.ms/efcore-docs-cosmos\">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.\n/// </remarks>\npublic static class CosmosIndexExtensions\n{\n    /// <summary>\n    ///     Returns the vector index type to use, such as \"flat\", \"diskANN\", or \"quantizedFlat\".\n    ///     See <see href=\"https://aka.ms/ef-cosmos-vectors\">Vector Search in Azure Cosmos DB for NoSQL</see> for more information.\n    /// </summary>\n    /// <param name=\"index\">The index.</param>\n    /// <returns>The index type to use, or <see langword=\"null\" /> if none is set.</returns>\n    public static VectorIndexType? GetVectorIndexType(this IReadOnlyIndex index)\n        => (index is RuntimeIndex)\n            ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)\n            : (VectorIndexType?)index[CosmosAnnotationNames.VectorIndexType];\n\n    /// <summary>\n    ///     Sets the vector index type to use, such as \"flat\", \"diskANN\", or \"quantizedFlat\".\n    ///     See <see href=\"https://aka.ms/ef-cosmos-vectors\">Vector Search in Azure Cosmos DB for NoSQL</see> for more information.\n    /// </summary>\n    /// <param name=\"index\">The index.</param>\n    /// <param name=\"indexType\">The index type to use.</param>\n    public static void SetVectorIndexType(this IMutableIndex index, VectorIndexType? indexType)\n        => index.SetOrRemoveAnnotation(CosmosAnnotationNames.VectorIndexType, indexType);\n\n    /// <summary>\n    ///     Sets the vector index type to use, such as \"flat\", \"diskANN\", or \"quantizedFlat\".\n    ///     See <see href=\"https://aka.ms/ef-cosmos-vectors\">Vector Search in Azure Cosmos DB for NoSQL</see> for more information.\n    /// </summary>\n    /// <param name=\"index\">The index.</param>\n    /// <param name=\"indexType\">The index type to use.</param>\n    /// <param name=\"fromDataAnnotation\">Indicates whether the configuration was specified using a data annotation.</param>","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Extensions/CosmosIndexExtensions.cs#L8-L44","documentation":"GetVectorIndexType() reads the Cosmos vector-index-type annotation from an index. The read-optimized runtime model (RuntimeIndex) deliberately omits design-time-only annotations to stay lean, so calling it on a RuntimeIndex throws InvalidOperationException via CoreStrings.RuntimeModelMissingData and directs you to the design-time model instead. Use DbContext.GetService<IDesignTimeModel>().Model for any annotation/configuration-source inspection.","triggerScenarios":"Calling index.GetVectorIndexType() where the index was obtained by navigating DbContext.Model (the runtime/cached model) — e.g. metadata inspection code, diagnostics, or a compiled-model path that hands back a RuntimeIndex.","commonSituations":"Building tooling/migrations that read index metadata at runtime; assuming DbContext.Model and the design-time model are equivalent; iterating model.GetEntityTypes().SelectMany(e => e.GetIndexes()) and reading Cosmos annotations.","solutions":["Obtain the index from the design-time model: var dt = dbContext.GetService<IDesignTimeModel>().Model; then navigate to the index and call GetVectorIndexType().","Never read design-time-only annotations (VectorIndexType, configuration sources) from DbContext.Model; treat the runtime model as read-optimized.","If you must branch on annotation presence at runtime, guard with `index is not RuntimeIndex` before calling."],"exampleFix":"// before (runtime model throws)\nvar t = context.Model\n    .FindEntityType(typeof(Item))!.GetIndexes().First().GetVectorIndexType();\n\n// after (design-time model)\nvar dtModel = context.GetService<IDesignTimeModel>().Model;\nvar t = dtModel\n    .FindEntityType(typeof(Item))!.GetIndexes().First().GetVectorIndexType();","handlingStrategy":"validation","validationCode":"// Guard before reading a design-time-only annotation.\nif (index is RuntimeIndex)\n{\n    var dtIndex = context.GetService<IDesignTimeModel>().Model\n        .FindEntityType(entityType)!.GetIndexes().ElementAt(i);\n    return dtIndex.GetVectorIndexType();\n}\nreturn index.GetVectorIndexType();","typeGuard":"static bool IsRuntimeIndex(IReadOnlyIndex index) => index is RuntimeIndex;","tryCatchPattern":"try { return index.GetVectorIndexType(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"read-optimized model\"))\n{ return context.GetService<IDesignTimeModel>().Model /* navigate to index */ .GetVectorIndexType(); }","preventionTips":["Always read Cosmos index annotations from IDesignTimeModel, not DbContext.Model.","Treat DbContext.Model as read-optimized for query execution only.","Guard annotation reads with `index is not RuntimeIndex` when the source is uncertain."],"tags":["cosmos","metadata","runtime-model","vector-search"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}