EF Core cannot match an entity constructor's parameters to its properties
Your error
No suitable constructor was found for entity type 'Blog'. The following constructors had parameters that could not be bound to properties of the entity type:2 more lines it can showFix it fast
Most common cause, 1 of 3A parameter's name or type does not match any property
Rename the parameter to the camelCase form of its property and give it the same type.
public class Post
{
public Post(string title, DateTime postedOn)
{
Title = title;
PostedOn = postedOn;
}
public int Id { get; private set; }
public string Title { get; private set; }
public DateTime PostedOn { get; private set; }
}Short answer
No suitable constructor was found for entity type (for the type in EF Core 10 and 11) means EF Core could not bind every constructor parameter to a mapped property. Typical causes: a differing name or type, a navigation property, or a get-only property EF never mapped. Rename or remove that parameter, or add a parameterless constructor.
Which cause do you have?
Read the Cannot bind part of the message: it names each failing parameter and the constructor it sits in. Then let the EF tools build the model again from the command line, so you can retry each change without clicking through the app.
dotnet ef migrations has-pending-model-changesThen find the line you see in this list and open its fix:
- 1A parameter's name or type does not match any property
The parameter named after Cannot bind is spelled differently from every property, or its type differs, for example int against long or string against an enum - 2The constructor takes a navigation property or an owned type
The failing parameter is another entity or a collection of them, such as Blog blog or ICollection<Post> posts, or a reference to an owned type - 3The matching property exists but has no setter, so it is not mapped
The names and types line up, but the property is declared with only a getter, such as public string Name { get; }
What does the error look like?
No suitable constructor was found for entity type 'Blog'. The following constructors had parameters that could not be bound to properties of the entity type:No suitable constructor was found for the type 'Blog'. The following constructors had parameters that could not be bound to properties of the type:Note that only mapped properties can be bound to constructor parameters. Navigations to related entities, including references to owned types, cannot be bound.
Cause 1 of 3
A parameter's name or type does not match any property
You seeThe parameter named after Cannot bind is spelled differently from every property, or its type differs, for example int against long or string against an enum
Check
EF Core only binds a constructor parameter when its type equals the property type and its name equals the property name. The only allowed difference is case: a PascalCase property such as PostedOn matches a camelCase parameter postedOn. Any other rename leaves the parameter unbound.
// property: public DateTime PostedOn { get; private set; }
// parameter: public Post(string title, DateTime published) <- no PostedOn matchFix
Rename the parameter to the camelCase form of its property and give it the same type. If the constructor needs a value that is not stored, keep that constructor for your own code and add a private parameterless one for EF Core.
public class Post
{
public Post(string title, DateTime postedOn)
{
Title = title;
PostedOn = postedOn;
}
public int Id { get; private set; }
public string Title { get; private set; }
public DateTime PostedOn { get; private set; }
}Verify
The EF command finishes without the constructor exception, and a query that loads the entity returns rows.
dotnet ef migrations has-pending-model-changesConfirmed by learn.microsoft.com
Cause 2 of 3
The constructor takes a navigation property or an owned type
You seeThe failing parameter is another entity or a collection of them, such as Blog blog or ICollection<Post> posts, or a reference to an owned type
Check
The error text says so directly: navigations to related entities, references to owned types included, cannot be bound. Microsoft's constructor guide repeats the rule from the other side: EF Core cannot set navigation properties through a constructor, so such a parameter can never be filled.
dotnet ef migrations has-pending-model-changesFix
Drop navigation parameters from any constructor EF Core may choose. Keep scalar values such as the foreign key, and let your code or EF set the navigation after the object exists.
public class Post
{
public Post(string title, int blogId)
{
Title = title;
BlogId = blogId;
}
public int Id { get; private set; }
public string Title { get; private set; }
public int BlogId { get; private set; }
public Blog Blog { get; set; }
}Verify
The has-pending-model-changes run gets past model building with no constructor exception, and new posts still save with their BlogId.
dotnet ef migrations has-pending-model-changesConfirmed by raw.githubusercontent.com
Cause 3 of 3
The matching property exists but has no setter, so it is not mapped
You seeThe names and types line up, but the property is declared with only a getter, such as public string Name { get; }
Check
Microsoft's constructor guide states that properties without setters are not mapped by convention, because that would also sweep in computed properties. An unmapped property cannot receive a constructor value, so its parameter fails even though the spelling is right.
grep -rnE "\{ get; \}" Models/Fix
Give the property a private setter, which EF Core treats as read-write, or keep it get-only and map it explicitly in OnModelCreating.
public string Name { get; private set; }
// or keep { get; } and map it:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(b => b.Property(e => e.Name));
}Verify
The model builds, and the Name column holds the value passed to the constructor after SaveChanges.
dotnet ef migrations has-pending-model-changesConfirmed by learn.microsoft.com
How do I stop it coming back?
- Give each entity a private parameterless constructor whenever its public constructor carries anything besides mapped scalar values.
- Run dotnet ef migrations has-pending-model-changes in CI, so a constructor EF Core cannot use fails the build instead of the first request.
- Use private set rather than get-only properties on entities, unless you also map those properties in OnModelCreating.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Raheeb, Loade Driver, Dhero.