dotnet/efcore · error · ArgumentException

The property '{property}' of the argument '{argument}' canno

Error message

The property '{property}' of the argument '{argument}' cannot be 'null'.

What it means

Thrown by the T4-based model generator when ModelCodeGenerationOptions.ContextName is null at code-generation time. The context name is required to name the generated DbContext class and its output file, so a null value is treated as an invalid argument rather than defaulting silently.

Source

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

        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)
        {
            throw new ArgumentException(
                CoreStrings.ArgumentPropertyNull(nameof(options.ConnectionString), nameof(options)), nameof(options));
        }

        var host = new TextTemplatingEngineHost(_serviceProvider)
        {
            Session =
            {
                { "Model", model },
                { "Options", options },
                { "NamespaceHint", options.ContextNamespace ?? options.ModelNamespace },
                { "ProjectDefaultNamespace", options.RootNamespace }
            }

View on GitHub (pinned to 3a2006ef56)

Solutions

  1. Set options.ContextName to a valid C# identifier before calling GenerateModel.
  2. Use the higher-level ScaffoldModel API, which derives a default context name from the database name when ContextName is empty.
  3. Pass --context <Name> on `dotnet ef dbcontext scaffold` to set it explicitly.

Example fix

// before
var options = new ModelCodeGenerationOptions();
sg.GenerateModel(model, options);
// after
var options = new ModelCodeGenerationOptions { ContextName = "ShopContext" };
sg.GenerateModel(model, options);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(options.ContextName))
    options.ContextName = "AppDbContext";
// or fail fast
ArgumentException.ThrowIfNullOrWhiteSpace(options.ContextName);

Prevention

When it happens

Trigger: Calling TextTemplatingModelGenerator.GenerateModel(model, options) with options.ContextName == null. Typically only reachable when invoking the T4 generator programmatically rather than through the scaffold command (which derives a context name first).

Common situations: Programmatic scaffolding where the caller forgot to set ContextName, or a custom design-time workflow that bypasses the default name derivation in ReverseEngineerScaffolder.

Related errors


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