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)
{
if(!optionsBuilder.IsConfigured)
{
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.AddMvc();
services.AddDbContext<thegoodyard.domain.Concrete.ThegoodyardContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ThegoodyardContext")));
}
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?
DbContextOptions<T>
in the parameter. The error message clearly states whats wrong, in case you are using EF Core: You did derieve from DbContext and either you only declared a parameterless constructor or you are using EF 6 – Tseng