{"record":{"id":"cb706233a3fe0d56","repo":"dotnet/efcore","slug":"dbset-local-symbol","errorCode":null,"errorMessage":"DbSet local symbol","messagePattern":"DbSet local symbol","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":430,"sourceCode":"            case ILocalSymbol s:\n                typeSymbol = s.Type;\n                break;\n            case IFieldSymbol s:\n                typeSymbol = s.Type;\n                break;\n            case IPropertySymbol s:\n                typeSymbol = s.Type;\n                break;\n            case null:\n                throw new InvalidOperationException($\"Identifier without symbol: {identifierName}\");\n            default:\n                throw new UnreachableException($\"IdentifierName of type {symbol.GetType().Name}: {identifierName}\");\n        }\n\n        // TODO: Separate out EF Core-specific logic (EF Core would extend this visitor)\n        if (typeSymbol.Name.Contains(\"DbSet\"))\n        {\n            throw new NotImplementedException(\"DbSet local symbol\");\n        }\n\n        // We have an identifier which isn't in our parameters stack.\n\n        // First, if the identifier type is the user's DbContext type (e.g. DbContext local variable, or field/property),\n        // return a constant over that.\n        if (typeSymbol.Equals(_userDbContextSymbol, SymbolEqualityComparer.Default))\n        {\n            return Constant(_userDbContext);\n        }\n\n        // The Translate entry point into the translator uses Roslyn's data flow analysis to locate all local variables flowing in\n        // (e.g. captured variables), and populates the _dataFlowsIn dictionary with them (with null values).\n        if (symbol is ILocalSymbol localSymbol && _dataFlowsIn.TryGetValue(localSymbol, out var memberExpression))\n        {\n            // The first time we see a flowing-in variable, we create MemberExpression for it and cache it in _dataFlowsIn.\n            return memberExpression\n                ?? (_dataFlowsIn[localSymbol] =","sourceCodeStart":412,"sourceCodeEnd":448,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L412-L448","documentation":"After binding an identifier to a local/field/property symbol, `VisitIdentifierName` checks `if (typeSymbol.Name.Contains(\"DbSet\"))` and throws `NotImplementedException(\"DbSet local symbol\")`. Referencing a `DbSet<T>` as a captured local variable or field inside a query is not supported — query roots must go through the `DbContext` itself.","triggerScenarios":"A precompiled query that captures a `DbSet<T>` into a local/field and then queries it, e.g. `var set = ctx.Blogs; ... set.Where(b => ...)` inside the translated lambda.","commonSituations":"Refactoring a query to reuse a `DbSet` variable, or helper methods that take/return `DbSet` locals used within translated expressions.","solutions":["Reference the `DbSet` directly off the `DbContext` inside the query (`ctx.Blogs.Where(...)`).","Pass the `DbContext` (which the translator resolves as a constant) instead of a `DbSet` local."],"exampleFix":"// before\nvar blogs = ctx.Blogs;\nvar q = blogs.Where(b => b.Title == \"x\");\n// after\nvar q = ctx.Blogs.Where(b => b.Title == \"x\");","handlingStrategy":"validation","validationCode":"// Reject captured DbSet locals/fields before translation.\nvar dbsetRefs = node.DescendantNodes().OfType<IdentifierNameSyntax>()\n    .Where(id => semanticModel.GetSymbolInfo(id).Symbol is { } s\n                 && s.Kind is SymbolKind.Local or SymbolKind.Field or SymbolKind.Property\n                 && (semanticModel.GetTypeInfo(id).Type?.Name.Contains(\"DbSet\") ?? false))\n    .ToList();\nif (dbsetRefs.Count != 0)\n    throw new NotSupportedException(\n        \"Captured DbSet locals/fields are unsupported; reference the DbSet directly off the DbContext.\");\n\ntranslator.Translate(node, semanticModel);","typeGuard":null,"tryCatchPattern":"try { translator.Translate(node, semanticModel); }\ncatch (NotImplementedException ex) when (ex.Message == \"DbSet local symbol\")\n{ /* rewrite the query to access ctx.Blogs directly */ }","preventionTips":["Reference DbSets directly from the DbContext inside queries (`ctx.Blogs.Where(...)`).","Do not extract `DbSet<T>` into a local/field and then use it inside the translated lambda.","Pass the `DbContext` into helpers rather than a `DbSet`."],"tags":["ef-core","precompiled-queries","expression-trees","dbset","unsupported-construct"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}