Search code examples
asp.net-mvcasp.net-corevisual-studio-2017entity-framework-core

Entity Framework Core Error: No parameterless constructor defined for this object


At the point of creating a new MVC Controller:

enter image description here

after I click Add button, I get the following Error:

enter image description here

Here is my simple Context class:

  public class MainDbContext : DbContext
  {
    public MainDbContext(DbContextOptions<MainDbContext> options) : base(options)
    {
    }

    public DbSet<Todo> Todo { get; set; }

  }

and my simple model:

public partial class Todo
{
    public int Id { get; set; }
    public string TaskName { get; set; }
}

I have made some search on this issue, most of the posts point to a dropdown list or a SelectList method using MVC, but for my case it is a Controller creation fail, so it seems to be an Entity Framework Core issue

Any help ?


Solution

  • Thanks to @poke comment above, and to this link: "Use Code First with connection by convention", by modifying the context class as follows C# will call base class parameterless constructor by default

      public class MainDbContext : DbContext
      {
        public MainDbContext()
        // C# will call base class parameterless constructor by default
        {
        }
      }