Skip to content

IDesignTimeDbContextFactory without a connection string #3706

@bdovaz

Description

@bdovaz

In other providers such as Microsoft.EntityFrameworkCore.Sqlite or Pomelo.EntityFrameworkCore.MySql, you can implement it by calling UseSqlite() or UseMySql() without parameters.

That is, without connecting to a real database.

With Npgsql.EntityFrameworkCore.PostgreSQL and UseNpgsql(), I get the following error:

Unable to create a 'DbContext' of type 'MyDbContext'. The exception 'The ConnectionString property has not been initialized.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Code:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

public class MybContextContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
    public MyDbContext CreateDbContext(string[] args)
    {
        DbContextOptionsBuilder<MyDbContext> dbContextOptionsBuilder = new();
        dbContextOptionsBuilder.UseNpgsql();

        return new MyDbContext(dbContextOptionsBuilder.Options);
    }
}

Command:

dotnet ef dbcontext script --output Databas.sql

cc @roji

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions