Asp.Net Core MVC, InvalidOperationException When Navigating to a Controller

I've created an Asp.Net core MVC application, database first. .NETCore.App (2.1).
It is the first MVC Core app that I've created.
When I run the application with IIS Express, it loads in the browser correctly.
https://localhost:44312/
Then I create a controller called ApplicantsController.
When I try to navigate to,
https://localhost:44312/Applicants
I get the following error:
InvalidOperationException: Unable to resolve service for type 'SalaryCalculator.Models.Salary_CalculatorContext' while attempting to activate 'SalaryCalculator.Controllers.ApplicantsController'.

Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

Here is the Startup.cs class:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }      

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

Open in new window

Babak SekandariProgrammer / AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Babak SekandariProgrammer / AnalystAuthor Commented:
I added the following and now it works:

            var connection = @"Server=ServerName; Database=DataBaseName; Trusted_Connection=true;";
            services.AddDbContext<Models.DataBaseNameContext>(options => options.UseSqlServer(connection));

Could someone please explain why this solution worked?
I mean, what's going on under the hood?
How come it didn't work before and this fixed it?
Babak SekandariProgrammer / AnalystAuthor Commented:
As noted above:
I added the following and now it works:

            var connection = @"Server=ServerName; Database=DataBaseName; Trusted_Connection=true;";
            services.AddDbContext<Models.DataBaseNameContext>(options => options.UseSqlServer(connection));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.