Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

Unable to create a 'DbContext' of type 'ApplicationDbContext' While trying to create migrations npgsql

$
0
0

I've been trying to switch SQL Server to PostgreSQL but I couldn't handle it. I'm following clean architecture.

This is the first error that I encountered:

AletheiaSoft.Infrastructure.Data.ApplicationDbContextInitialiser[0] An error occurred while initialising the database.
System.TypeInitializationException: The type initializer for 'Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping.NpgsqlBigIntegerTypeMapping' threw an exception

Then I realized this is because of migrations. I just disable migrating and seeding so only thing that working was connecting. But afterwards I just got this error:

Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time

That was all error message.

Dependency injection:

using AletheiaSoft.Application.Common.Interfaces;using AletheiaSoft.Domain.Constants;using AletheiaSoft.Infrastructure.Data;using AletheiaSoft.Infrastructure.Data.Interceptors;using AletheiaSoft.Infrastructure.Identity;using Microsoft.AspNetCore.Identity;using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Diagnostics;using Microsoft.Extensions.Configuration;namespace Microsoft.Extensions.DependencyInjection;public static class DependencyInjection{    public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)    {        var connectionString = configuration.GetConnectionString("PostgreSqlConnection");        Guard.Against.Null(connectionString, message: "Connection string 'DefaultConnection' not found.");        services.AddScoped<ISaveChangesInterceptor, AuditableEntityInterceptor>();        services.AddScoped<ISaveChangesInterceptor, DispatchDomainEventsInterceptor>();        services.AddDbContext<ApplicationDbContext>((sp, options) =>        {            options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());            options.UseNpgsql(connectionString);        });        services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());        services.AddScoped<ApplicationDbContextInitialiser>();        services.AddAuthentication()            .AddBearerToken(IdentityConstants.BearerScheme);        services.AddAuthorizationBuilder();        services            .AddIdentityCore<ApplicationUser>()            .AddRoles<IdentityRole>()            .AddEntityFrameworkStores<ApplicationDbContext>()            .AddApiEndpoints();        services.AddSingleton(TimeProvider.System);        services.AddTransient<IIdentityService, IdentityService>();        services.AddAuthorization(options =>            options.AddPolicy(Policies.CanPurge, policy => policy.RequireRole(Roles.Administrator)));        return services;    }}

DbContext:

using System.Reflection;using AletheiaSoft.Application.Common.Interfaces;using AletheiaSoft.Domain.Entities;using AletheiaSoft.Infrastructure.Identity;using Microsoft.AspNetCore.Identity.EntityFrameworkCore;using Microsoft.EntityFrameworkCore;namespace AletheiaSoft.Infrastructure.Data;public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IApplicationDbContext{    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }    public DbSet<TodoList> TodoLists => Set<TodoList>();    public DbSet<TodoItem> TodoItems => Set<TodoItem>();    public DbSet<Client> Clients => Set<Client>();    public DbSet<Project> Projects => Set<Project>();    protected override void OnModelCreating(ModelBuilder builder)    {        base.OnModelCreating(builder);        builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());    }}

My stack is :

  • Dotnet 8.0
  • PostgreSql : 8.0

I tried it with SQL Server package (just to test DbContext).

SQL Server could handle with it. Also connection string of Postgresql is correct.


Viewing all articles
Browse latest Browse all 12201

Trending Articles