public User(){}
I got the same issue when trying to Add-Migration on the DbContext class.
First I got the error: Unable to create an object of type 'ContextDb'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Which made me add the interface IDesignTimeDbContextFactory and implement it's function:
public ContextDb CreateDbContext(string[] args){var optionBuilder = new DbContextOptionsBuilder<ContextDb>();optionBuilder.UseSqlServer("Server_connection");return new ContextDb(optionBuilder.Options);}
At this time the error No parameterless constructor defined for type 'LibraryContext.ContextDb' happened when trying to Add-Migration again. Which occured due to the new instance of the ContextDb created in CreateDbContext().To fix this I just added an empty constructor:
public ContextDb() {}
This error started for me when I added a new way to instantiate a class.
Example:
public class myClass{public string id{ get; set; }public List<string> myList{get; set;}// error happened after I added thispublic myClass(string id, List<string> lst){this.id= id;this.myList= lst;}}
The error was resolved when I added when I made this change, adding a parameterless constructor. I believe the compiler creates a parameterless constuctor by default but if you add your own then you must explicitly create it.
public class myClass{public string id{ get; set; }public List<string> myList{get; set;}// error doesn't happen when I add thispublic myClass() { }// error happened after I added this, but no longer happens after adding abovepublic myClass(string id, List<string> lst){this.id= id;this.myList= lst;}}
I'd added a DropDownList
to my form, however in my case it wasn't (and wasn't intended to be) submitted with the form as it was outside of the <form></form>
tags:
@Html.DropDownList("myField", Model.MyField)
As the Model contained the field for display only, this also caused the No parameterless constructor defined for this object
error because the field wasn't submitted at all.
In this case I fixed it by adding an exclude binding:
public ActionResult Foo(int id, int? page, [Bind(Exclude = "MyField")]MyModel model)
This happened to me, and the results on this page were a good resource that led me in many directions, but I would like to add another possibility:
As stated in other replies, creating a constructor with parameters removes the implicit parameterless constructor, so you have to explicitly type it.
What was my problem was that a constructor with default parameters also triggered this exception.
Gives errors:
public CustomerWrapper(CustomerDto customer = null){...}
Works:
public CustomerWrapper(CustomerDto customer){...}public CustomerWrapper():this(null){}
Most probably you might have parameterized constructor in your controller and whatever dependency resolver you are using is not able to resolve the dependency properly. You need to put break-point where the dependency resolver method is written and you will get the exact error in inner exception.
I had the same problem.
Just Removed HttpFileCollectionBase files
from Post Action method argument and added like HttpFileCollectionBase files = Request.Files;
in method body.
So I have gotten that message before as well, when doing an ajax call. So what it's basically asking for is a constructor in that model class that is being called by the contoller, doesn't have any parameter.
Here is an example
public class MyClass{public MyClass(){} // so here would be your parameterless constructor}