skip to Main Content

I read book Ultimate ASP.NET Core Web API. And there is a code first database creation using DbContext and IDesignTimeDbContextFactory<>.

public class RepositoryContextFactory : IDesignTimeDbContextFactory<RepositoryContext> 
{ 
       public RepositoryContext CreateDbContext(string[] args) 
       { 
              var configuration = new ConfigurationBuilder() 
                     .SetBasePath(Directory.GetCurrentDirectory()) 
                     .AddJsonFile("appsettings.json") 
                     .Build(); 
 
              var builder = new DbContextOptionsBuilder<RepositoryContext>() 
        
       .UseSqlServer(configuration.GetConnectionString("sqlConnection")); 
 
              return new RepositoryContext(builder.Options); 
       } 
} 

The code is working for creating migration files. But I need this for Postgres – not MS SQL. And next code is not going to create migaration files. It terminates with the error: Unable to create a ‘DbContext’ of type ”. The exception ‘Method not found: ‘Void Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping..ctor(System.Stri
ng, System.Type, System.Nullable1<System.Data.DbType>, Boolean, System.Nullable1, Boolean, System.Nullable1<Int32>, System.Nullable1)’.’ was thro
wn while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Repository;
using Npgsql;

namespace CompanyEmployees.ContextFactory;

public class RepositoryContextFactory: IDesignTimeDbContextFactory<RepositoryContext>
{
    public RepositoryContext CreateDbContext(string[] args)
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

        var builder = new DbContextOptionsBuilder<RepositoryContext>()
            .UseNpgsql(configuration.GetConnectionString("PostgresConnection"), 
                b => b.MigrationsAssembly("CompanyEmployees"));

        return new RepositoryContext(builder.Options);
    }
}

More of that I don’t understand – why Rider IDE shows using Npgsql; as unused namespace while I explicitly use method UseNpgsql()…
Please give some hint – ChatGPT is not into it. Long time – no idea what to fix.

In appsettings.json connection string is
"ConnectionStrings": {
"PostgresConnection": "Host=localhost;Port=5432;User Id=postgres;Password=postgres; Database=CompanyEmployees"
}
Or without Database parameter – both doesn’t help.

2

Answers


  1. Chosen as BEST ANSWER

    Hmm... It appeared that I needed to downgrade both EntitityFramework.Core and Tools and Npgsql to major 7 version - due to .net version 7.0.

    Though I still don't get - why Rider shows using Npgsql; as unused.


  2. For first one you need to install **Microsoft.EntityFrameworkCore.SqlServer**
    **dotnet add package Microsoft.EntityFrameworkCore.SqlServer**
    or Install-Package Microsoft.EntityFrameworkCore.SqlServer
    
    -------------------------------------------------------------------
    
    You need to get **Npgsql.EntityFrameworkCore.PostgreSQL** from nuget package for second one.
    **Install-Package Npgsql.EntityFrameworkCore.PostgreSQL**
    or **dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL**
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search