数据库设置中的asp.net-core迁移错误

ramet 发布于 4 天前 database 最后更新 4 天前 1 浏览

我试图先用asp.net core api来使用代码。 执行Add-Migration MyFirstMigration时,出现错误 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.GetOrAddTKey,TValue,TArg at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.EntityFrameworkCore.Design.DbContextOperations.<>cDisplayClass13_2.<FindContextTypes>b6() 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.<>cDisplayClass0_0.<Configure>b0() 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类看起来像

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喜欢
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();
        }
在project.json工具中
"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-*"
  },
已邀请:

lquia

赞同来自:

DbContextOptions参数应为DbContextOptions<DataBaseContext>而不是DbContextOptions<DbContext>。 更改DataBaseContext类的构造函数。

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