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

13,426

Solution 1

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
    {
    }
  }

Solution 2

It's a tooling error. Most likely, you're running Visual Studio 2015, which doesn't have full .NET Core support. Basically, in previous versions of EF, DbContext had a parameterless constructor, and this version of the scaffold generator is depending on that. In EF Core, DbContext does not have a parameterless constructor, so the generator is choking on that.

If you're using VS2015, upgrade to 2017. It's time. Aside from that, you don't need this anyways, and it's only leading you down a bad path. All the scaffold does is create a new class under Controller, named {Name}Controller that inherits from Controller. Then it creates a folder named {Name} in Views and adds some basic HTML for doing CRUD. You'll end up replacing most of this HTML anyways. Additionally, the scaffold requires you to work with an actual entity class, which is the last thing you should ever be doing. You should always accept user input via a view model and then map that posted data onto your entity class before finally saving the entity. Look at the scaffold being broken as an excellent opportunity to start learning how to create good code.

Solution 3

Here's the solution from Microsoft. It suggest to create a design-time class that instantiates the connection to a database.

Share:
13,426

Related videos on Youtube

Sami-L
Author by

Sami-L

Updated on June 04, 2022

Comments

  • Sami-L
    Sami-L almost 2 years

    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 ?

  • Sami-L
    Sami-L over 6 years
    Great advises, currently I am using Asp.Net Core 2.0 / Entity Framework Core 2.0 with Visual Studio 2017. It seems these newly changed technologies still have alot of lacks, and I move very slowly because of issues.
  • Gert Arnold
    Gert Arnold over 2 years
    This can't have been the same problem. This has nothing to do with EF contexts.