Home > Software engineering >  AddDbContext does not get initialized when running ASP.NET Web API
AddDbContext does not get initialized when running ASP.NET Web API

Time:12-15

I'm in the process of building a user management microservice using an API.NET Web API. I've added EntityFrameworkCore and using SQL Server as the database. I've made multiple services at this point, but this service does not seem to work the way I want it to.

FYI I'm using .NET 6.

The code you see below is how I add the AddDbContext to my Services in the Program.cs file.

builder.Services.AddDbContext<AppDbContext>(opt =>
{
    // Getting the ConnectionString from the AppSettings-file.
    string connectionString = builder.Configuration.GetConnectionString("Connection1");

    Console.WriteLine("=====CONNECTION STRING=====");
    Console.WriteLine("ConnectionString: "   connectionString   "\n");
    Console.WriteLine("=====IT Asset Management=====");
    Console.WriteLine("Service running: User Service");

    if (builder.Environment.IsProduction())
        Console.WriteLine("Environment is: PRODUCTION");
    else
        Console.WriteLine("Environment is: DEVELOPMENT");

    // Configuring the DBContext to connect to the SQL server.
    opt.UseSqlServer(connectionString);
});

When i run the code, my Console.WriteLines does not print, and it does not seem that the AddDbContext service gets initialized.

Below is my DbContext.

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using UserService.Models;

namespace UserService.Data
{
    public class AppDbContext : IdentityDbContext<AppUser>
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
                        : base(options) { }
    }
}

As of now I have tried everything I can think of and gotten help from colleagues, but we can't seem to find out what's wrong. I have another service running the exact same code, but with a different purpose (Only the Program.cs files are the same), so I've tried to copy paste the file. I have tried to reinstall all the related NuGet-packages, which did not work.

Then i tried to create a new ASP.NET Web API project and only installing the packages needed to make the DbContext and setup the service in the Program.cs file. Below is the Program.cs file from the new project.

The funny thing is that it does not work aswell in the new project and I have no idea why.

using UserManagement.Data;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<AppDbContext>(opt =>
{
    Console.WriteLine("=====IT Asset Management=====");
    Console.WriteLine("Service running: User Service");
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

I cannot think of anything I have done differently between these three projects.

UPDATE 1: I found this post: AddDbContext doesn't call DbContext constructor

and one of the answers was that I needed to use migrations. I have not migrated anything from this project, since there is no tables to be made. I am using the default Identity tables, and they are created by another service. But when I added the migrations the Console.WriteLines got printed. Then i ran the project with "dotnet run" but then the prints were gone. Why is this?

CodePudding user response:

So I seemed that one of the answers inside the post in UPDATE 1, actually was the solution to the issue. I did indeed have to use the DbContext in some context. I added a dataseeding class which is called from the program.cs file, and it seems that the console.writelines are printed every time now.

  • Related