dotnet/efcore · error · OperationException

You've provided an EntityTypeConfiguration.t4 file without a

Error message

You've provided an EntityTypeConfiguration.t4 file without a corresponding DbContext.t4 file. The generated DbContext code must be modified to work with your configuration classes. Provide a DbContext.t4 file and try again.

What it means

Thrown when EF Core detects an EntityTypeConfiguration.t4 template but no DbContext.t4 template in the project's CodeTemplates directory. The generated DbContext must emit calls to apply configuration classes; without a custom DbContext.t4 those calls cannot be wired up, so EF refuses the incomplete template set.

Source

Thrown at src/EFCore.Design/Scaffolding/Internal/TextTemplatingModelGenerator.cs:66

    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected virtual TemplatingEngine Engine
        => _engine ??= new TemplatingEngine();

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override bool HasTemplates(string projectDir)
    {
        var hasContextTemplate = File.Exists(Path.Combine(projectDir, TemplatesDirectory, DbContextTemplate));
        var hasEntityTypeTemplate = File.Exists(Path.Combine(projectDir, TemplatesDirectory, EntityTypeTemplate));
        var hasConfigurationTemplate = File.Exists(Path.Combine(projectDir, TemplatesDirectory, EntityTypeConfigurationTemplate));

        return hasConfigurationTemplate && !hasContextTemplate
            ? throw new OperationException(DesignStrings.NoContextTemplateButConfiguration)
            : hasContextTemplate || hasEntityTypeTemplate || hasConfigurationTemplate;
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationOptions options)
    {
        if (options.ContextName == null)
        {
            throw new ArgumentException(
                CoreStrings.ArgumentPropertyNull(nameof(options.ContextName), nameof(options)), nameof(options));
        }

        if (options.ConnectionString == null)

View on GitHub (pinned to 3a2006ef56)

Solutions

  1. Add a DbContext.t4 template alongside EntityTypeConfiguration.t4 in the same CodeTemplates folder.
  2. Copy the default DbContext.t4 from the EF Core templates and extend it to apply your configuration classes.
  3. If you did not intend the configuration style, remove the EntityTypeConfiguration.t4 file.
  4. Keep the template set consistent: include both DbContext.t4 and EntityTypeConfiguration.t4 (and optionally EntityType.t4).

Example fix

Project/
  CodeTemplates/
    EFCore/
      EntityTypeConfiguration.t4   # present
      DbContext.t4                 # ADD THIS
      EntityType.t4                # optional
Defensive patterns

Strategy: validation

Validate before calling

bool hasConfig = File.Exists(Path.Combine(projectDir, "CodeTemplates", "EFCore", "EntityTypeConfiguration.t4"));
bool hasCtx   = File.Exists(Path.Combine(projectDir, "CodeTemplates", "EFCore", "DbContext.t4"));
if (hasConfig && !hasCtx)
    throw new InvalidOperationException("Add DbContext.t4 to match EntityTypeConfiguration.t4");

Prevention

When it happens

Trigger: Calling HasTemplates(projectDir) and the directory contains EntityTypeConfiguration.t4 (or its .t4 variant) but not DbContext.t4 under the project's CodeTemplates/<language>/ folder.

Common situations: Adopting the entity-type-configuration scaffolding style (one config class per entity) but forgetting to also supply a DbContext template that applies those configurations via model.ApplyConfigurations or HasConstructor parameters.

Related errors


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