无法创建 MyDbContext 类型的对象
Unable to create an object of type MyDbContext
我正在使用 Autofac 注册 MyDbContext
,它工作正常,但是当我尝试使用任何 ef 迁移命令(例如,Add-Migration -Name my-migration
)时,我收到错误:
Unable to create an object of type 'MyDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
如果我用 IServiceCollection
注册 MyDbContext
,错误就会消失。我的 Autofac 配置是:
builder.Register(
_ =>
{
var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
var conn = configuration.GetConnectionString(connName);
var dbConnection = new NpgsqlConnection(conn);
var password = configuration.GetPassword(connName);
dbConnection.ProvidePasswordCallback =
(host, port, database, username) => password;
optionsBuilder.UseNpgsql(dbConnection);
return new MyDbContext(optionsBuilder.Options);
})
.AsSelf()
.InstancePerLifetimeScope();
根据微软文档:
The tools first try to obtain the service provider by invoking Program.CreateHostBuilder(), calling Build(), then accessing the Services property.
我调试了我的应用程序,我能够从通过调用 IHostBuilder.Build()
获得的 IHost
激活 MyDbContext
。我缺少什么,我需要做其他事情吗?
提前致谢。
您是否尝试过提到的解决方案
builder.Register(componentContext =>
{
var serviceProvider = componentContext.Resolve<IServiceProvider>();
var configuration = componentContext.Resolve<IConfiguration>();
var dbContextOptions = new DbContextOptions<TContext>(new Dictionary<Type, IDbContextOptionsExtension>());
var optionsBuilder = new DbContextOptionsBuilder<TContext>(dbContextOptions)
.UseApplicationServiceProvider(serviceProvider)
.UseSqlServer(configuration.GetConnectionString("MyConnectionString"),
serverOptions => serverOptions.EnableRetryOnFailure(5, TimeSpan.FromSeconds(30), null));
return optionsBuilder.Options;
}).As<DbContextOptions<TContext>>()
.InstancePerLifetimeScope();
builder.Register(context => context.Resolve<DbContextOptions<TContext>>())
.As<DbContextOptions>()
.InstancePerLifetimeScope();
builder.RegisterType<TContext>()
.AsSelf()
.InstancePerLifetimeScope();
我正在使用 Autofac 注册 MyDbContext
,它工作正常,但是当我尝试使用任何 ef 迁移命令(例如,Add-Migration -Name my-migration
)时,我收到错误:
Unable to create an object of type 'MyDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
如果我用 IServiceCollection
注册 MyDbContext
,错误就会消失。我的 Autofac 配置是:
builder.Register(
_ =>
{
var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
var conn = configuration.GetConnectionString(connName);
var dbConnection = new NpgsqlConnection(conn);
var password = configuration.GetPassword(connName);
dbConnection.ProvidePasswordCallback =
(host, port, database, username) => password;
optionsBuilder.UseNpgsql(dbConnection);
return new MyDbContext(optionsBuilder.Options);
})
.AsSelf()
.InstancePerLifetimeScope();
根据微软文档:
The tools first try to obtain the service provider by invoking Program.CreateHostBuilder(), calling Build(), then accessing the Services property.
我调试了我的应用程序,我能够从通过调用 IHostBuilder.Build()
获得的 IHost
激活 MyDbContext
。我缺少什么,我需要做其他事情吗?
提前致谢。
您是否尝试过提到的解决方案
builder.Register(componentContext =>
{
var serviceProvider = componentContext.Resolve<IServiceProvider>();
var configuration = componentContext.Resolve<IConfiguration>();
var dbContextOptions = new DbContextOptions<TContext>(new Dictionary<Type, IDbContextOptionsExtension>());
var optionsBuilder = new DbContextOptionsBuilder<TContext>(dbContextOptions)
.UseApplicationServiceProvider(serviceProvider)
.UseSqlServer(configuration.GetConnectionString("MyConnectionString"),
serverOptions => serverOptions.EnableRetryOnFailure(5, TimeSpan.FromSeconds(30), null));
return optionsBuilder.Options;
}).As<DbContextOptions<TContext>>()
.InstancePerLifetimeScope();
builder.Register(context => context.Resolve<DbContextOptions<TContext>>())
.As<DbContextOptions>()
.InstancePerLifetimeScope();
builder.RegisterType<TContext>()
.AsSelf()
.InstancePerLifetimeScope();