{"record":{"id":"a12a4f4d3b2ff5fe","repo":"dotnet/efcore","slug":"the-following-file-s-already-exist-in-directory","errorCode":null,"errorMessage":"The following file(s) already exist in directory '{outputDirectoryName}': {existingFiles}. Use the Force flag to overwrite these files.","messagePattern":"The following file\\(s\\) already exist in directory '(.+?)': (.+?)\\. Use the Force flag to overwrite these files\\.","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"warning","filePath":"src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs","lineNumber":179,"sourceCode":"        foreach (var path in paths)\n        {\n            var fullPath = Path.Combine(outputDir, path);\n\n            if (File.Exists(fullPath))\n            {\n                existingFiles.Add(path);\n\n                if (File.GetAttributes(fullPath).HasFlag(FileAttributes.ReadOnly))\n                {\n                    readOnlyFiles.Add(path);\n                }\n            }\n        }\n\n        if (!overwriteFiles\n            && existingFiles.Count != 0)\n        {\n            throw new OperationException(\n                DesignStrings.ExistingFiles(\n                    outputDir,\n                    string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, existingFiles)));\n        }\n\n        if (readOnlyFiles.Count != 0)\n        {\n            throw new OperationException(\n                DesignStrings.ReadOnlyFiles(\n                    outputDir,\n                    string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, readOnlyFiles)));\n        }\n    }\n}\n","sourceCodeStart":161,"sourceCodeEnd":194,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs#L161-L194","documentation":"Thrown during scaffolding when one or more generated output files already exist in the target output directory and the Force/overwrite option is not set. EF Core refuses to overwrite to prevent destroying user-edited generated code; it lists the colliding files and points to the --force flag.","triggerScenarios":"Running `dotnet ef dbcontext scaffold` (or ScaffoldModel with overwriteFiles=false) into a directory that already contains a file with the same name as a file about to be written (DbContext or entity classes).","commonSituations":"Re-running scaffold into the same project folder where a previous run left generated files. Moving an existing DbContext into the output path before re-scaffolding. Source-controlled generated files that were not deleted between runs.","solutions":["Pass --force to `dotnet ef dbcontext scaffold` (or set ModelCodeGenerationOptions with overwriteFiles=true / use the Force flag) to allow overwrites.","Delete or move the existing files listed in the error message out of the output directory.","Scaffold into a fresh subdirectory to diff against the existing code.","Add the generated files to .gitignore if they are pure regeneration, or keep them tracked and always use --force."],"exampleFix":"// before\ndotnet ef dbcontext scaffold \"...\" Microsoft.EntityFrameworkCore.SqlServer\n// after\ndotnet ef dbcontext scaffold \"...\" Microsoft.EntityFrameworkCore.SqlServer --force","handlingStrategy":"validation","validationCode":"string dir = options.ContextDir ?? options.ProjectDir;\nvar willCollide = generatedFileNames.Any(f => File.Exists(Path.Combine(dir, f)));\nif (willCollide && !options.OverwriteFiles)\n{\n    // either delete or set overwrite\n    options.OverwriteFiles = true;\n}","typeGuard":null,"tryCatchPattern":"try { scaffolder.ScaffoldModel(...); }\ncatch (OperationException ex) when (ex.Message.Contains(\"already exist\"))\n{\n    Console.Error.WriteLine(\"Re-run with --force, or remove: \" + ex.Message);\n}","preventionTips":["Always scaffold into a fresh subdirectory during exploration.","Use --force once generated files are under source control and you intend to regenerate.","Add generated files to .gitignore if they are pure regeneration artifacts."],"tags":["scaffolding","reverse-engineering","filesystem","design-time"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}