{"record":{"id":"57904dd1962611be","repo":"elsa-workflows/elsa-core","slug":"register-nameof-aidbcontext-with-configured-nameof","errorCode":null,"errorMessage":"Register {nameof(AIDbContext)} with configured {nameof(DbContextOptions<AIDbContext>)} before calling {nameof(AddAIPersistenceStores)}, or call {nameof(AddAIPersistenceStores)} with a database provider configuration.","messagePattern":"Register (.+?) with configured (.+?) before calling (.+?), or call (.+?) with a database provider configuration\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/modules/Elsa.AI.Persistence.EFCore/Extensions/ServiceCollectionExtensions.cs","lineNumber":20,"sourceCode":"using Elsa.AI.Persistence.EFCore;\nusing Elsa.AI.Persistence.EFCore.Services;\nusing Elsa.AI.Persistence.EFCore.Stores;\nusing Microsoft.EntityFrameworkCore;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.DependencyInjection.Extensions;\nusing Microsoft.Extensions.Hosting;\n\n// ReSharper disable once CheckNamespace\nnamespace Elsa.Extensions;\n\npublic static class ServiceCollectionExtensions\n{\n    public static IServiceCollection AddAIPersistenceStores(this IServiceCollection services, Action<DbContextOptionsBuilder>? configureDbContext = null)\n    {\n        if (!services.Any(x => x.ServiceType == typeof(DbContextOptions<AIDbContext>)))\n        {\n            if (configureDbContext == null)\n                throw new InvalidOperationException($\"Register {nameof(AIDbContext)} with configured {nameof(DbContextOptions<AIDbContext>)} before calling {nameof(AddAIPersistenceStores)}, or call {nameof(AddAIPersistenceStores)} with a database provider configuration.\");\n            else\n                services.AddDbContext<AIDbContext>(configureDbContext);\n        }\n\n        services.TryAddScoped<AIDbContext>();\n        services.AddAIPersistenceStoreServices();\n\n        return services;\n    }\n\n    internal static IServiceCollection AddAIPersistenceStoreServices(this IServiceCollection services)\n    {\n        services.Replace(ServiceDescriptor.Scoped<IAIConversationStore, EFCoreAIConversationStore>());\n        services.Replace(ServiceDescriptor.Scoped<IAIProposalStore, EFCoreAIProposalStore>());\n        services.TryAddEnumerable(ServiceDescriptor.Scoped<IAIAuditEventHandler, EFCoreAIAuditSink>());\n        services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, EFCoreAIConversationCleanupService>());\n\n        return services;","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.AI.Persistence.EFCore/Extensions/ServiceCollectionExtensions.cs#L2-L38","documentation":"AddAIPersistenceStores needs a configured DbContextOptions<AIDbContext> in the service collection. If none is registered and no configureDbContext delegate is supplied, it throws InvalidOperationException because EF Core could not build AIDbContext. The method can either receive a provider configuration delegate or find pre-registered DbContext options.","triggerScenarios":"Calling AddAIPersistenceStores() with no arguments when DbContextOptions<AIDbContext> was never registered (e.g. AddDbContext<AIDbContext> or AddAIPersistenceStores(s => s.UseSqlite(...)) was not called).","commonSituations":"Forgetting the database provider in DI setup; calling AddAIPersistenceStores before AddDbContext in a different module; copy-pasted setup that dropped the UseSqlite/UseSqlServer call.","solutions":["Pass a provider configuration: AddAIPersistenceStores(o => o.UseSqlite(connectionString)).","Register options first via AddDbContext<AIDbContext>(...) before calling AddAIPersistenceStores().","Check the order of extension calls so the options registration happens earlier in the pipeline."],"exampleFix":"// before\nservices.AddAIPersistenceStores();\n// after\nservices.AddAIPersistenceStores(options => options.UseSqlite(\"Data Source=elsa-ai.db\"));","handlingStrategy":"validation","validationCode":"var hasOptions = services.Any(x => x.ServiceType == typeof(DbContextOptions<AIDbContext>));\nif (!hasOptions)\n    services.AddAIPersistenceStores(o => o.UseSqlite(connectionString));\nelse\n    services.AddAIPersistenceStores();","typeGuard":null,"tryCatchPattern":"try { services.AddAIPersistenceStores(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"AddAIPersistenceStores\")) { services.AddAIPersistenceStores(o => o.UseSqlite(connectionString)); }","preventionTips":["Always pass a provider configuration delegate unless options are registered elsewhere.","Register database provider configuration in a single shared setup method.","Check module initialization order in DI composition roots."],"tags":["dotnet","ef-core","dependency-injection","configuration"],"backgroundTag":"missing-required-config","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}