我正在尝试使用Cosmos DB for Database创建基于.Net Core 3和EF Core 3的应用程序。
我已经按照以下步骤操作了,
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>();
}
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContextFactory >
{
public MyDbContextFactory CreateDbContext(string[] args)
{
// Get environment
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
// Build config
var config = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.AddEnvironmentVariables()
.Build();
var cosmosDbOptions = config.GetSection("CosmosDb").Get<CosmosDbOptions>();
var (cosmosDbEndpoint, cosmosDbKey, databaseName, collectionData) = cosmosDbOptions;
var collectionNames = collectionData.Select(c => c.Name).ToList();
var optionsBuilder = new DbContextOptionsBuilder<MyDbContextFactory>();
optionsBuilder.UseCosmos(cosmosDbEndpoint, cosmosDbKey, databaseName);
return new ActivityLoggerContext(optionsBuilder.Options);
}
和
public class MyDbContextFactory : DbContext
{
public MyDbContextFactory (DbContextOptions<MyDbContextFactory > dbContextOptions) : base(dbContextOptions)
{
}
public DbSet<UserModel> Users { get; set; }
}
当我执行命令时,
> dotnet ef database update
我收到以下错误。任何帮助或指针将不胜感激
System.InvalidOperationException:无法解析“ Microsoft.EntityFrameworkCore.Migrations.IMigrator”类型的服务。这通常是因为尚未为此DbContext配置任何数据库提供程序。可以通过重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,则还应确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。
在Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService [TService](IInfrastructure 1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure
1访问器)处
在Microsoft.EntityFrameworkCore.Design.DesignTimeServiceCollectionExtensions。<> c__DisplayClass1_0.b__7(IServiceProvider _)
在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite,RuntimeResolverContext上下文)
在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor
2.VisitCallSite(ServiceCallSite callSite,TArgument参数)处
在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite,ServiceProviderEngineScope范围)
在Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine。<> c__DisplayClass1_0.b__0(ServiceProviderEngineScope范围)
在Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(类型serviceType,ServiceProviderEngineScope serviceProviderEngineScope)
在Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType)
在Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型serviceType)
在Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService [T](IServiceProvider提供程序)
在Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.EnsureServices(IServiceProvider服务)
在Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration,String contextType)
在Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration,String contextType)
在Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase。<> c__DisplayClass0_0。<。ctor> b__0()
在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action操作)
无法解析类型为“ Microsoft.EntityFrameworkCore.Migrations.IMigrator”的服务。这通常是因为尚未为此DbContext配置任何数据库提供程序。可以通过重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,则还要确保您的DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。
答案 0 :(得分:0)
基于异常消息,您需要在添加dbcontext服务时或在dbcontext的OnConfiguring
方法中配置数据库提供程序。
通过参考以下教程,我获得了成功:Using DbContext with dependency injection
在该教程中,它使用SqlLite,您可以通过添加Microsoft.EntityFrameworkCore.Cosmos