No suitable constructor found for entity type string
This is the same issue I got while adding a new property in my entity and updated the existing constructor and passed new property as a parameter
Fix: Instead of updating an existing constructor, added an overloaded constructor with a new property and this error was gone while creating migration
Just had a similar problem.
I had a class and was doing some changes and deleted the default class constructor. All though it is never called EF still needs it or you will get a No suitable constructor found exception
public class Company
{
public Company ( )
{
// ef needs this constructor even though it is never called by
// my code in the application. EF needs it to set up the contexts
// Failure to have it will result in a
// No suitable constructor found for entity type 'Company'. exception
}
public Company ( string _companyName , ......)
{
// some code
}
}
This has been answered partially, but there is a general guideline in EF Core that ought to be stated with regard to this problem.
When you defined an Entity in an Entity class, you need to ensure that the entire thing is public and accessable. For example,
public class GroupMap
{
[Key] public int Id { get; set; }
public string GroupId { get; set; }
}
There are two properties, Id (which has a redundant [Key] annotation) and GroupId. Just thinking about GroupId, it must be a public property, and it must have both its getter and setter defined.
So this will fail:
public class GroupMap
{
[Key] public int Id { get; set; }
private string GroupId { get; set; } // private property not allowed
}
As will this:
public class GroupMap
{
[Key] public int Id { get; set; }
public string GroupId { get; } // setter must be defined
}
Since EF Core is ORM, you also need to make sure that the property type makes sense with regard to the database (though I can't provide much detail here). Finally, as stated elsewhere, you also need to provide the correct context definition. All the same rules apply.
For example:
public DbSet<Item> Items { get; set; } // public, and getter & setter defined
As also stated elsewhere, the DbSet type needs to be a concrete class that defines the various properties you wish to be either columns in your table, or relations to another table.
Hope that helps someone out there.
The problem is in your context, you have this line:
public DbSet<string> Codes { get; set; }
You need to use a concrete class for your entities, a string
cannot be used.