Entity Framwork Core Add-Migration fails

.net-core asp.net-core-2.0 c# ef-core-2.1 entity-framework-core


I'm trying to use Ef Core in my project.

The structure is a little different, in the sense that I'm not using EfCore insite the WebApi.csproj. In fact I have a different dll. and a DependenciesResolver.dll that handles all my dependency injection.

In my EfCore.dll I've installed both



Now when I try to run the command (the dll in which I'm running is the EfCore.dll)

Add-Migration Name

I get this :

An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Object reference not set to an instance of an object. Unable to create an object of type 'StoreContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

The structure of the sln is like this

WebApi | EfCore.dll | DependencyResolver.dll and I want to keep it this way, don't want to permit using EfCore in my WebApi.

What is the resolution for this issue ?

If this helps within the EfCore.dll I have this.

 public sealed partial class StoreContext : DbContext, IStoreContext
        private string _connectionString;

        public StoreContext(string connectionString) : base()
            _connectionString = connectionString;


        /// db.tbls
        public DbSet<Order> Orders { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

which is called by DependencyResolver like this

        private DependenciesResolver RegisterInfrastructure()
            _serviceCollection.AddScoped<StoreContext>(factory => new StoreContext(_connectionString));

            return this;

and the DependencyResolver is then called by the WebApi

9/21/2018 12:14:28 PM

Accepted Answer

Please have a look at this: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation

The error message clearly specifies the EF Core tools can't create an instance of your Context at design-time.

If you can define a constructor with no parameters for your StoreContext that would work, otherwise you need tell the tools how to create an instance of your context at design-time by defining a class that implements the IDesignTimeDbContextFactory interface.

9/21/2018 1:21:51 PM

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