{"record":{"id":"47d6811ce0d243f8","repo":"dotnet/efcore","slug":"the-context-class-name-contextclassname-is-not","errorCode":null,"errorMessage":"The context class name '{contextClassName}' is not a valid C# Identifier.","messagePattern":"The context class name '(.+?)' is not a valid C# Identifier\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs","lineNumber":77,"sourceCode":"    private IModelCodeGeneratorSelector ModelCodeGeneratorSelector { get; }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual ScaffoldedModel ScaffoldModel(\n        string connectionString,\n        DatabaseModelFactoryOptions databaseOptions,\n        ModelReverseEngineerOptions modelOptions,\n        ModelCodeGenerationOptions codeOptions)\n    {\n        if (!string.IsNullOrWhiteSpace(codeOptions.ContextName)\n            && (!_cSharpUtilities.IsValidIdentifier(codeOptions.ContextName)\n                || _cSharpUtilities.IsCSharpKeyword(codeOptions.ContextName)))\n        {\n            throw new ArgumentException(\n                DesignStrings.ContextClassNotValidCSharpIdentifier(codeOptions.ContextName));\n        }\n\n        var resolvedConnectionString = _connectionStringResolver.ResolveConnectionString(connectionString);\n        if (resolvedConnectionString != connectionString)\n        {\n            codeOptions.SuppressConnectionStringWarning = true;\n        }\n        else if (!codeOptions.SuppressOnConfiguring)\n        {\n            _reporter.WriteWarning(DesignStrings.SensitiveInformationWarning);\n        }\n\n        codeOptions.ConnectionString ??= connectionString;\n\n        var databaseModel = _databaseModelFactory.Create(resolvedConnectionString, databaseOptions);\n        var modelConnectionString = (string?)databaseModel[ScaffoldingAnnotationNames.ConnectionString];\n        if (!string.IsNullOrEmpty(modelConnectionString))","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs#L59-L95","documentation":"Thrown in ReverseEngineerScaffolder.ScaffoldModel (line 77, resource ContextClassNotValidCSharpIdentifier) when the supplied context name is not a valid C# identifier or is a C# keyword. EF must emit a class with that name, so it validates via IsValidIdentifier and IsCSharpKeyword; failing either throws ArgumentException before any database work. Only checked when ContextName is non-blank.","triggerScenarios":"Calling ScaffoldModel (or 'dotnet ef dbcontext scaffold --context <name>') with a ContextName that contains invalid identifier characters, starts with a digit, or equals a reserved keyword. The condition `!IsValidIdentifier || IsCSharpKeyword` is true and throws ContextClassNotValidCSharpIdentifier(ContextName).","commonSituations":"Passing a context name with spaces, dots, hyphens, or special characters (e.g. 'My-Context', '1Context'). Using a C# keyword as the name (e.g. 'class', 'event', 'namespace'). Deriving the name from a database/schema name with illegal characters without sanitizing.","solutions":["Choose a context name composed of letters/digits/underscores that starts with a letter or underscore (e.g. 'ShopContext').","Avoid C# reserved keywords as the context name.","If the name is derived programmatically, sanitize it (strip illegal chars, prefix with underscore) before passing it in.","Leave ContextName blank to let ReverseEngineerScaffolder derive a valid default from the database name."],"exampleFix":"// before: illegal context name\ndotnet ef dbcontext scaffold \"...\" SqlServer --context My-Context\n\n// after: valid identifier\ndotnet ef dbcontext scaffold \"...\" SqlServer --context MyContext","handlingStrategy":"validation","validationCode":"// Validate the context name before scaffolding\nvar utils = new CSharpUtilities();\nif (!string.IsNullOrWhiteSpace(name)\n    && (!utils.IsValidIdentifier(name) || utils.IsCSharpKeyword(name)))\n    throw new ArgumentException($\"'{name}' is not a valid C# identifier.\");","typeGuard":"static bool IsValidContextName(string name)\n    => !string.IsNullOrWhiteSpace(name)\n       && System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentIdentifier(name)\n       && !CSharpKeywords.Contains(name);","tryCatchPattern":"try { scaffolder.ScaffoldModel(...); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"valid C# identifier\"))\n{ /* sanitize the name (strip invalid chars, prefix underscore) and retry */ }","preventionTips":["Pass an identifier-safe context name (letters/digits/underscore, leading letter/underscore).","Avoid C# keywords as names.","Sanitize names derived from database/schema identifiers.","Leave ContextName blank to use a derived default."],"tags":["ef-core","scaffolding","reverse-engineer","validation","design-time"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}