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

  1. Pass --force to `dotnet ef dbcontext scaffold` (or set ModelCodeGenerationOptions with overwriteFiles=true / use the Force flag) to allow overwrites.
  2. Delete or move the existing files listed in the error message out of the output directory.
  3. Scaffold into a fresh subdirectory to diff against the existing code.
  4. 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

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


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/a12a4f4d3b2ff5fe. Report an issue: GitHub.