Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net core在启动时获取DbContext实例,使用DbContext调用服务方法_.net_Entity Framework_Dependency Injection_Startup_Dbcontext - Fatal编程技术网

.net core在启动时获取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

我已将DbContext连接到数据库。该上下文在启动时配置为依赖项注入

   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