dotnet/efcore · error · ArgumentException

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

Error message

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

What it means

Thrown by CSharpModelGenerator.GenerateModel when ModelCodeGenerationOptions.ContextName is null. Scaffolding (reverse engineering) needs a class name for the generated DbContext, so a missing ContextName is treated as a bad argument and reported as ArgumentException via CoreStrings.ArgumentPropertyNull.

Solutions

  1. Set options.ContextName to a valid class name before calling GenerateModel.
  2. If invoking via CLI, pass --context <Name> (or use the default derived from the database).
  3. Validate required options up front in your tooling wrapper.

Example fix

// before
var options = new ModelCodeGenerationOptions();
// after
var options = new ModelCodeGenerationOptions { ContextName = "AppDbContext" };
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(options.ContextName))
    throw new ArgumentException("ContextName is required for scaffolding.");
// then call generator.GenerateModel(model, options);

Type guard

static bool IsScaffoldOptionsValid(ModelCodeGenerationOptions o) => !string.IsNullOrWhiteSpace(o.ContextName);

Try / catch

try { model = generator.GenerateModel(model, options); }
catch (ArgumentException ex) when (ex.Message.Contains("ContextName"))
{ /* prompt the user for a context name and retry */ }

Prevention

When it happens

Trigger: Calling the scaffolding model generator (directly or via 'dotnet ef dbcontext scaffold' / Scaffold-DbContext) without setting the ContextName property on ModelCodeGenerationOptions.

Common situations: Programmatic scaffolding invocation that forgets ContextName; a custom tooling host that constructs ModelCodeGenerationOptions with defaults; misconfigured scaffolding options.

Related errors


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

Appendix: source

Thrown at src/EFCore.Design/Scaffolding/Internal/CSharpModelGenerator.cs:58

    ///     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 string Language
        => "C#";

    /// <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);
        var contextTemplate = new CSharpDbContextGenerator { Host = host, Session = host.CreateSession() };
        contextTemplate.Session.Add("Model", model);
        contextTemplate.Session.Add("Options", options);
        contextTemplate.Session.Add("NamespaceHint", options.ContextNamespace ?? options.ModelNamespace);
        contextTemplate.Session.Add("ProjectDefaultNamespace", options.RootNamespace);
        contextTemplate.Initialize();

        var generatedCode = ProcessTemplate(contextTemplate);

View on GitHub (pinned to 3a2006ef56)