'How do you add a service to Program.cs using EF Core 6?

In EF Core 6 they have combined the Startup.cs and Program.cs files into just Program.cs. I followed the following tutorial: https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-6.0 and added builder.Services.AddScoped<IMovieService, MovieService>(); to my Program.cs file but I get this:

`System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: movie_library.core.Services.IMovieService Lifetime: Scoped ImplementationType: movie_library.core.MovieService': Unable to resolve service for type 'movie_library.data.MovieLibraryContext' while attempting to activate 'movie_library.core.MovieService'.)'

I'm just trying to call my service in my api controller using dependency injection.
My Controller:

enter image description here

My Program.cs File:

enter image description here



Solution 1:[1]

I think you would need something like this:

        var connectionString = Configuration.GetConnectionString("MovieDatabase");


        services.AddDbContextFactory<MovieLibraryContext >(options => options.UseSqlServer(connectionString));

But it's hard to tell exactly without seeing your MovieService.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Sinnema