Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

A 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.

Fix
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; }
}
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to EF Core 11.0.0, EF Core 10.0.15, .NET 11 and .NET 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
dotnet ef migrations has-pending-model-changes

Then find the line you see in this list and open its fix:

  1. 1A parameter's name or type does not match any propertyThe 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
  2. 2The constructor takes a navigation property or an owned typeThe 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
  3. 3The matching property exists but has no setter, so it is not mappedThe 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.

Check
// property:  public DateTime PostedOn { get; private set; }
// parameter: public Post(string title, DateTime published)  <- no PostedOn match

Fix

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.

Fix
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.

Verify
dotnet ef migrations has-pending-model-changes

Confirmed by learn.microsoft.com

Did this fix it?

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.

Check
dotnet ef migrations has-pending-model-changes

Fix

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.

Fix
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.

Verify
dotnet ef migrations has-pending-model-changes

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
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.

Fix
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.

Verify
dotnet ef migrations has-pending-model-changes

Confirmed by learn.microsoft.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.