2016-09-26 5 views
1

asp.net core apiで最初にコードを使用しようとしています。私はエラーデータベース設定時のasp.net-coreの移行エラー

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'.

マイDatabaseContextクラスはproject.jsonツールで

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(); 
     } 

よう

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のように見える取得Add-Migration MyFirstMigrationを実行すると

"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" 
    }, 
を持って

この記事にはhttps://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.htmlといくつかの異なるものがありますが、それを理解していません。たぶん誰かが助けてくれると思いますか?

編集部分:

"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-*" 
    }, 
+0

project.jsonの依存関係セクションにEntityFrameworkCoreパッケージが記載されていますか? –

+0

投稿する依存関係セクションを追加しました。すべての必要なパッケージが必要です。 – martinv

答えて

0

DbContextOptionsパラメータはDbContextOptions<DataBaseContext>の代わりDbContextOptions<DbContext>でなければなりません。

DataBaseContextクラスのコンストラクタを変更します。

public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options) 
{ 
} 
+0

ありがとう、これはそれを解決しました。 – martinv

関連する問題