dotnet/efcore · warning · OperationException
The following file(s) already exist in directory '{outputDir
Error message
The following file(s) already exist in directory '{outputDirectoryName}': {existingFiles}. Use the Force flag to overwrite these files. What it means
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.
Source
Thrown at src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs:179
foreach (var path in paths)
{
var fullPath = Path.Combine(outputDir, path);
if (File.Exists(fullPath))
{
existingFiles.Add(path);
if (File.GetAttributes(fullPath).HasFlag(FileAttributes.ReadOnly))
{
readOnlyFiles.Add(path);
}
}
}
if (!overwriteFiles
&& existingFiles.Count != 0)
{
throw new OperationException(
DesignStrings.ExistingFiles(
outputDir,
string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, existingFiles)));
}
if (readOnlyFiles.Count != 0)
{
throw new OperationException(
DesignStrings.ReadOnlyFiles(
outputDir,
string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, readOnlyFiles)));
}
}
}
View on GitHub (pinned to 3a2006ef56)
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.
Example fix
// before dotnet ef dbcontext scaffold "..." Microsoft.EntityFrameworkCore.SqlServer // after dotnet ef dbcontext scaffold "..." Microsoft.EntityFrameworkCore.SqlServer --force
Defensive patterns
Strategy: validation
Validate before calling
string dir = options.ContextDir ?? options.ProjectDir;
var willCollide = generatedFileNames.Any(f => File.Exists(Path.Combine(dir, f)));
if (willCollide && !options.OverwriteFiles)
{
// either delete or set overwrite
options.OverwriteFiles = true;
} Try / catch
try { scaffolder.ScaffoldModel(...); }
catch (OperationException ex) when (ex.Message.Contains("already exist"))
{
Console.Error.WriteLine("Re-run with --force, or remove: " + ex.Message);
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- No files were generated in directory '{outputDirectoryName}'
- Metadata model returned should not be 'null'. Provider: {pro
- The literal expression '{expression}' for '{type}' cannot be
- Cannot scaffold C# literals of type '{literalType}'. The pro
- A type-qualified method call requires an instance identifier
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/a12a4f4d3b2ff5fe.
Report an issue: GitHub.