The Scoped Singleton DI Bug Your AI Just Suggested Common software bug where a Scoped service (like `OrderService`) caches an entity from a Scoped `DbContext` into a Singleton service (like `IMemoryCache`). This causes data corruption and crashes in production because the cached entity holds a reference to a `DbContext` that has been disposed after the initial request ends. The author explains that AI assistants frequently suggest this flawed pattern because their training data often omits dependency injection lifetime registrations, and provides rules to prevent the bug, such as ensuring cached data is detached from its original context. The Scoped→Singleton DI bug your AI just suggested and how to catch it Of all the bugs that ship to production silently, the captured-dependency lifetime bug is one of the most expensive. It compiles. It passes your tests. It runs fine in dev. Then in production, under load, it starts corrupting data across requests. And AI assistants suggest it constantly. Here's why — and the one Cursor rule that catches it before merge. The bug, in 30 lines You ask Cursor to add caching to OrderService. It gives you this: plaintext // OrderService.cs public class OrderService : IOrderService { private readonly IMemoryCache cache; private readonly OrderDbContext db; public OrderService IMemoryCache cache, OrderDbContext db { cache = cache; db = db; } public async Task