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 in CSharpModelGenerator.GenerateModel (line 58) when ModelCodeGenerationOptions.ContextName is null. The scaffolding/reverse-engineering code generator needs a context class name to emit the DbContext file, so a null name is rejected with ArgumentException before any template runs. It is a contract violation on the options object passed to GenerateModel.
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 dbf9771522)
Solutions
- Set options.ContextName to a valid, non-null context class name before calling GenerateModel.
- If scaffolding programmatically, derive a default name (e.g. from the database name) and assign it, mirroring ReverseEngineerScaffolder's behavior.
- Validate all required ModelCodeGenerationOptions fields before invoking the generator.
Example fix
// before
var options = new ModelCodeGenerationOptions { ConnectionString = cs };
modelGenerator.GenerateModel(model, options); // throws: ContextName null
// after
var options = new ModelCodeGenerationOptions
{
ConnectionString = cs,
ContextName = "ShopContext"
};
modelGenerator.GenerateModel(model, options); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(options.ContextName))
throw new InvalidOperationException("ContextName is required.");
// or set a default before calling the generator Try / catch
try { generator.GenerateModel(model, options); }
catch (ArgumentException ex) when (ex.Message.Contains("ContextName"))
{ /* set options.ContextName and retry */ } Prevention
- Always set ContextName on ModelCodeGenerationOptions before GenerateModel.
- Centralize options construction in a helper that enforces required fields.
When it happens
Trigger: Calling GenerateModel (directly or via ScaffoldModel/ReverseEngineer) with options.ContextName left null. The check `if (options.ContextName == null)` fires and throws ArgumentException referencing CoreStrings.ArgumentPropertyNull(nameof(options.ContextName), nameof(options)).
Common situations: Programmatic scaffolding where the caller forgot to set ContextName on ModelCodeGenerationOptions. A wrapper/wizard that builds options incrementally and skips the name. Passing default(options)-style object. Note: ReverseEngineerScaffolder will derive a default ContextName, so this typically surfaces only when calling the code generator directly.
Related errors
- The context class name '{contextClassName}' is not a valid C
- Processing '{inputFile}' failed.
- Metadata model returned should not be 'null'. Provider: {pro
- You cannot add a migration with the name 'Migration'.
- The name '{migrationName}' is used by an existing migration.
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/0832134b1aed8bb9.
Report an issue: GitHub.