<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont" -->

---
title: Mixing AddDbContext and AddDbContextFactory: 'Cannot...
description: When registering both `AddDbContext` and `AddDbContextFactory` for the same DbContext in ASP.NET Core, the app fails to start with a 'Cannot consume scoped...
canonical: https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Mixing AddDbContext and AddDbContextFactory: 'Cannot consume scoped service' | daily.dev
og:description: When registering both `AddDbContext` and `AddDbContextFactory` for the same DbContext in ASP.NET Core, the app fails to start with a 'Cannot consume scoped...
og:url: https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont
og:image: https://api.daily.dev/og/posts/FC6e3HONT.png
og:image:alt: Mixing AddDbContext and AddDbContextFactory: 'Cannot consume scoped service'
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Mixing AddDbContext and AddDbContextFactory: 'Cannot consume scoped service'

**[The Art of Simplicity](https://daily.dev/sources/bartwullems)** · 2 min read · 13 upvotes · 1 comments

## Summary

When registering both `AddDbContext` and `AddDbContextFactory` for the same DbContext in ASP.NET Core, the app fails to start with a 'Cannot consume scoped service from singleton' error. The root cause is that `AddDbContext` registers `DbContextOptions<TContext>` with a scoped lifetime, while `AddDbContextFactory` registers `IDbContextFactory<TContext>` as a singleton — and singletons cannot depend on scoped services. The fix is to pass `optionsLifetime: ServiceLifetime.Singleton` to `AddDbContext`, which changes only the options lifetime (not the DbContext itself), aligning it with what the factory needs.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://bartwullems.blogspot.com/2026/08/mixing-adddbcontext-and.html>

## Questions this post answers

### Why do I get 'Cannot consume scoped service' when registering both AddDbContext and AddDbContextFactory for the same DbContext?

This happens because AddDbContextFactory registers IDbContextFactory as a singleton, but AddDbContext registers DbContextOptions with a scoped lifetime by default, and a singleton cannot depend on a scoped service. The dependency injection container throws this validation error at startup because the singleton factory can't consume the scoped options.

_Debugging DI lifetime conflicts in EF Core comes up often for readers tracking .NET issues on daily.dev._

### How do I fix the 'singleton cannot consume scoped service' error with EF Core DbContextFactory?

Pass optionsLifetime: ServiceLifetime.Singleton to the AddDbContext call so DbContextOptions is registered as a singleton too, matching what AddDbContextFactory expects. This only changes the lifetime of DbContextOptions, not the DbContext itself, so the scoped AddDbContext registration still hands out a scoped context per request.

_Developers wiring up EF Core dependency injection can find fixes like this through daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 1 upvotes

> Could the sample add a startup test that resolves both registrations from the real service provider?

## Similar posts on daily.dev

- [12 Rules for Dependency Injection in ASP.NET Core](https://daily.dev/posts/12-rules-for-dependency-injection-in-asp-net-core-nirfrs5ef) · We Are .NET · 3 upvotes · 1 comments
- [Catching dependency injection configuration errors early](https://daily.dev/posts/catching-dependency-injection-configuration-errors-early-4uupdrvtp) · The Art of Simplicity · 1 upvotes · 0 comments
- [DbContext is Not Thread-Safe: Parallelizing EF Core Queries the Right Way](https://daily.dev/posts/dbcontext-is-not-thread-safe-parallelizing-ef-core-queries-the-right-way-tlmjtq5g2) · Milan Jovanović · 4 upvotes · 0 comments
- [How to use keyed services in ASP.NET Core](https://daily.dev/posts/how-to-use-keyed-services-in-asp-net-core-sn17l40rp) · InfoWorld · 2 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#dependency-injection](https://daily.dev/tags/dependency-injection), [#aspnetcore](https://daily.dev/tags/aspnetcore)

[View this post on daily.dev](https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Mixing AddDbContext and AddDbContextFactory: 'Cannot consume scoped service'","url":"https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont"},"datePublished":"2026-08-12T05:19:57.164Z","dateModified":"2026-09-13T21:46:12.218Z","description":"When registering both `AddDbContext` and `AddDbContextFactory` for the same DbContext in ASP.NET Core, the app fails to start with a 'Cannot consume scoped...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/591434db253f82ea36a0e03fc7bb1645?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/591434db253f82ea36a0e03fc7bb1645?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"The Art of Simplicity","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"The Art of Simplicity","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/52b68c70636d44f0901ae7cd107c9438","url":"https://daily.dev/sources/bartwullems"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":13},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":".net,dependency-injection,aspnetcore","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"The Art of Simplicity","item":"https://daily.dev/sources/bartwullems"},{"@type":"ListItem","position":3,"name":"Mixing AddDbContext and AddDbContextFactory: 'Cannot consume scoped service'"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont","comment":[{"@type":"Comment","text":"Could the sample add a startup test that resolves both registrations from the real service provider?","datePublished":"2026-08-15T21:21:42.906Z","url":"https://daily.dev/posts/FC6e3HONT#c-NugHzQpoW","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/mixing-adddbcontext-and-adddbcontextfactory-cannot-consume-scoped-service--fc6e3hont#faq","mainEntity":[{"@type":"Question","name":"Why do I get 'Cannot consume scoped service' when registering both AddDbContext and AddDbContextFactory for the same DbContext?","acceptedAnswer":{"@type":"Answer","text":"This happens because AddDbContextFactory registers IDbContextFactory as a singleton, but AddDbContext registers DbContextOptions with a scoped lifetime by default, and a singleton cannot depend on a scoped service. The dependency injection container throws this validation error at startup because the singleton factory can't consume the scoped options. Debugging DI lifetime conflicts in EF Core comes up often for readers tracking .NET issues on daily.dev."}},{"@type":"Question","name":"How do I fix the 'singleton cannot consume scoped service' error with EF Core DbContextFactory?","acceptedAnswer":{"@type":"Answer","text":"Pass optionsLifetime: ServiceLifetime.Singleton to the AddDbContext call so DbContextOptions is registered as a singleton too, matching what AddDbContextFactory expects. This only changes the lifetime of DbContextOptions, not the DbContext itself, so the scoped AddDbContext registration still hands out a scoped context per request. Developers wiring up EF Core dependency injection can find fixes like this through daily.dev."}}]}
```

