我正在尝试先将代码与 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.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 类看起来像
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; }
}
像配置服务
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-*"
},
最佳答案
DbContextOptions
参数应为 DbContextOptions<DataBaseContext>
而不是 DbContextOptions<DbContext>
.
更改 DataBaseContext
的构造函数类。
public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options)
{
}
关于database - 数据库设置上的 asp.net-core 迁移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39705396/