在尝试确保已创建数据库时,我在statup类中执行以下操作:
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<MyContext>();
context.Database.EnsureCreated();
}
我已经通过以下方式在ServiceCollection中注册了上下文:
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));
致电EnsureCreated()
时出现以下异常:
DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[mynamespace.Data.MyDbContext])' on type 'MyDbContext'. ---> Entry point was not found. (See inner exception for details.)
DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = ChtrDbContext (ReflectionActivator), Services = [chtr.server.data.Data.ChtrDbContext], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[mynamespace.MyDbContext])' on type 'MyDbContext'. ---> Entry point was not found. (See inner exception for details.) (See inner exception for details.)
值得注意的是,我使用Autofac作为IoC框架