.net core在启动时获取DbContext实例,使用DbContext调用服务方法
我已将DbContext连接到数据库。该上下文在启动时配置为依赖项注入.net core在启动时获取DbContext实例,使用DbContext调用服务方法,.net,entity-framework,dependency-injection,startup,dbcontext,.net,Entity Framework,Dependency Injection,Startup,Dbcontext,我已将DbContext连接到数据库。该上下文在启动时配置为依赖项注入 public static class MyPhotoServiceCollectionExtension { public static IServiceCollection AddMyPhoto(this IServiceCollection services, IConfiguration configuration) { s
public static class MyPhotoServiceCollectionExtension
{
public static IServiceCollection AddMyPhoto(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<MyPhotoDbContext>(options =>
{
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
});
return services;
}
}
我试图在启动时调用AddDataToSearchDocuments()方法,我只需要执行一次,但它不起作用,因为它需要上下文。你有什么想法吗
我试过了
SearchDataService service=new SearchDataService(MyPhotoDbContext _context)
未给出与“MyPhotoDbContext.MyPhotoDbContext(DbContextOptions)”MyPhoto所需的形式参数“options”相对应的参数
SearchDataService service=new SearchDataService(MyPhotoDbContext _context)
There is no argument given that corresponds to the required formal parameter 'options' of 'MyPhotoDbContext.MyPhotoDbContext(DbContextOptions<MyPhotoDbContext>)' MyPhoto