1

Am trying to use code first with asp.net core api. When executing Add-Migration MyFirstMigration I get error

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions'1[Microsoft.EntityFrameworkCore.DbContext]' while attempting to activate 'DAL.DataBaseContext'. at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet'1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet'1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet'1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet '1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider) at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary'2 dictionary, TKey key, Func'3 valueFactory, TArg arg) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.EntityFrameworkCore.Design.DbContextOperations.<>c__DisplayClass13_2.<FindContextTypes>b__6() at Microsoft.EntityFrameworkCore.Design.DbContextOperations.CreateContext(Func'1 factory) at Microsoft.EntityFrameworkCore.Design.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String environment, Action'1 reporter) at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.<Configure>b__0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions'1[Microsoft.EntityFrameworkCore.DbContext]' while attempting to activate 'DAL.DataBaseContext'.

My DatabaseContext class looks like

public class DataBaseContext: DbContext, IDbContext
{
    public DataBaseContext(DbContextOptions<DbContext> options) : base(options)
    {
    }

    public DbSet<Location> Locations { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Event> Events { get; set; }
    public DbSet<ExistingProduct> ExisitingProducts { get; set; }
}  

ConfigService like

public void ConfigureServices(IServiceCollection services)
        {
            //Add framework services.
            var connection = @"Server=(LocalDb)\\v11.0;Database=Home;Trusted_Connection=True;MultipleActiveResultSets=true";
            services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connection));
            services.AddMvc();
        }

In project.json tools has

"tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

Looked into this article https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html and several different ones more but haven't figured it out. Maybe someone can help or point out what am i doing wrong?

Edited part:

"dependencies": {
    "Microsoft.NETCore.App": "1.0.1",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore": "1.0.1",
    "DAL": "1.0.0-*",
    "BLL": "1.0.0-*"
  },
2
  • Does the dependencies section of project.json list any EntityFrameworkCore packages? Commented Sep 26, 2016 at 18:51
  • I added dependencies section to post, it should have all needed packages. Commented Sep 27, 2016 at 6:39

1 Answer 1

1

The DbContextOptions parameter should be DbContextOptions<DataBaseContext> instead of DbContextOptions<DbContext>.

Change the constructor of your DataBaseContext class.

public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options)
{
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.