Unable to migrate DbContext class in Entity Framework Core

asp.net asp.net-core entity-framework-core


I am now developing a web api using ASP.NET core web api. This is my first time using ASP.NET core. I was an ASP.NET MVC developer. But been a away for a while. What I am doing now is creating a DbContext class for Entity Framework running the migration command. But I am using three different project to separate the logic.

This is my project structure:

[![enter image description here][1]][1]

I installed following packages.

Install-Package Microsoft.EntityFrameworkCore.SqlServer  -Projectname thegoodyard.api
Install-Package Microsoft.EntityFrameworkCore.Tools  -Projectname thegoodyard.api
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design  -Projectname thegoodyard.api
Install-Package Microsoft.EntityFrameworkCore.SqlServer  -Projectname thegoodyard.domain
Install-Package Microsoft.EntityFrameworkCore.Tools  -Projectname thegoodyard.domain
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design  -Projectname thegoodyard.domain

In the thegoodyard.domain project, I created a DB context class called ThegoodyardContext.cs with the following definition.

namespace thegoodyard.domain.Concrete
    public virtual DbSet<Category> Categories { get; set; }

    public partial class ThegoodyardContext: DbContext
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
                optionsBuilder.UseSqlServer(@"Server=(localdb)\\mssqllocaldb;Database=ThegoodyardContext;Trusted_Connection=True; MultipleActiveResultSets=true");

        protected override void OnModelCreating(ModelBuilder modelBuilder)

Then in the Thegoodyard.api project, in the ConfigureServices method of the StartUp class, I registered the context like this:

public void ConfigureServices(IServiceCollection services)

     services.AddDbContext<thegoodyard.domain.Concrete.ThegoodyardContext>(options =>

Then I enabled the migration running the following command

enable-migration -projectname thegoodyard.api

But it said, the command is too old, use the add-migration instead. I used this instead.

add-migration CreateCategory

Then I got the following error

An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: AddDbContext was called with configuration, but the context type 'ThegoodyardContext' only declares a parameterless constructor. This means that the configuration passed to AddDbContext will never be used. If configuration is passed to AddDbContext, then 'ThegoodyardContext' should declare a constructor that accepts a DbContextOptions and must pass it to the base constructor for DbContext.

No DbContext was found in assembly 'thegoodyard.api'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

How can I fix it?

9/16/2018 11:52:56 AM

Accepted Answer

Open Package manager console

Default Project : "Select your layer name", like : thegoodyard.domain then


Please check this link http://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx

9/16/2018 11:17:48 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow