.net Core 6 - Unable to resolve service for type while attempting to activate

In my ASP.NET Core application, I get the following error:

InvalidOperationException: Unable to resolve service for type 'mvc_net6.Service.DB_Context' while attempting to activate 'mvc_net6.Controllers.StudentController'.

The StudentController StudentController was created through Scaffolds to the view like so:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using mvc_net6.Models;
using mvc_net6.Service;

namespace mvc_net6.Controllers
{
    public class StudentController : Controller
    {
        private readonly DB_Context _context;

        public StudentController(DB_Context context)
        {
            _context = context;
        }

        // GET: Student
        public async Task<IActionResult> Index()
        {
            return View(await _context.Students.ToListAsync());
        }

        // GET: Student/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var student = await _context.Students
                .FirstOrDefaultAsync(m => m.Id == id);
            if (student == null)
            {
                return NotFound();
            }

            return View(student);
        }

        // GET: Student/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: Student/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,CPF,Email")] Student student)
        {
            if (ModelState.IsValid)
            {
                _context.Add(student);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(student);
        }

        // GET: Student/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var student = await _context.Students.FindAsync(id);
            if (student == null)
            {
                return NotFound();
            }
            return View(student);
        }

        // POST: Student/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Name,CPF,Email")] Student student)
        {
            if (id != student.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(student);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StudentExists(student.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(student);
        }

        // GET: Student/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var student = await _context.Students
                .FirstOrDefaultAsync(m => m.Id == id);
            if (student == null)
            {
                return NotFound();
            }

            return View(student);
        }

        // POST: Student/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var student = await _context.Students.FindAsync(id);
            _context.Students.Remove(student);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool StudentExists(int id)
        {
            return _context.Students.Any(e => e.Id == id);
        }
    }
}

I have one file DB_Context.cs that contains an class and its implementation like so:

using Microsoft.EntityFrameworkCore;
using mvc_net6.Models;

namespace mvc_net6.Service
{
    public class DB_Context : DbContext
    {
        public DB_Context(DbContextOptions<DB_Context> options) : base(options) {}

        public DbSet<mvc_net6.Models.Student> Students {get; set;}

    }

}

I have one file DB_Factory.cs that contains an class and its implementation like so:

namespace mvc_net6
{

   public class BloggingContextFactory : IDesignTimeDbContextFactory<DB_Context>
   {
      public DB_Context CreateDbContext(string[] args)
      {
         // Replace with your server version and type.
         // Use 'MariaDbServerVersion' for MariaDB.
         // Alternatively, use 'ServerVersion.AutoDetect(connectionString)'.
         var serverVersion = new MariaDbServerVersion(new Version(10, 5, 12));

         var optionsBuilder = new DbContextOptionsBuilder<DB_Context>();
         optionsBuilder.UseMySql("ConnectionString", serverVersion);


         return new DB_Context(optionsBuilder.Options);
      }
   }

}

My Program.cs contains the default-generated code from the template.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();
IConfiguration configuration = app.Configuration;

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}


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

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();


//builder.Services.AddDbContext<DB_Context>(options =>
//    options.UseMySql(connection, serverVersion));
 
builder.Services.AddDbContext<DB_Context>(opt =>
          {
            opt.UseMySql(connection, serverVersion);
          });

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

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
    

app.Run();

However, when running the system I now get and following error:

watch : Started
Unhandled exception. System.InvalidOperationException: Cannot modify ServiceCollection after application is built.
   at Microsoft.AspNetCore.WebApplicationServiceCollection.CheckServicesAccess()
   at Microsoft.AspNetCore.WebApplicationServiceCollection.Add(ServiceDescriptor item)
   at Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(IServiceCollection collection, ServiceDescriptor descriptor)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddCoreServices[TContextImplementation](IServiceCollection serviceCollection, Action`2 optionsAction, ServiceLifetime optionsLifetime)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContextService,TContextImplementation](IServiceCollection serviceCollection, Action`2 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContextService,TContextImplementation](IServiceCollection serviceCollection, Action`1 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContext](IServiceCollection serviceCollection, Action`1 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
   at Program.<Main>$(String[] args) in /home/r31sr4r/Projects/dotnet_core/mvc_net6/Program.cs:line 31

watch : Exited with error code 134
watch : Waiting for a file to change before restarting dotnet...

Solution 1:

in your program.cs, make sure to inject the db DB_Context

builder.services.AddDbContext<DB_Context>(opt =>
            {
                opt.UseMySql("YOUR_CONNECTION_STRING");
            });

otherwise, you can't use it on the controller

Solution 2:

Put your dbcontext config before this:

var app = builder.Build();

Solution 3:

You need to do this injections before the "var app = builder.Build();".