Home:ALL Converter>EF Core - Connection string cannot be null

EF Core - Connection string cannot be null

Ask Time:2018-12-12T23:05:00         Author:Kgn-web

Json Formatter

I am using ASP.Net Core 2.1 with EF Core 2.1

While running any Migration from PMC, I am getting the below error

Value cannot be null. Parameter name: connectionString

This is how my Context class looks like

public class MyAppContextFactory : IDesignTimeDbContextFactory<MyAppContext>
{
    private readonly string _dbConnection;

    public MyAppContextFactory()
    {

    }
    public MyAppContextFactory(string dbConnection)
        : this()
    {
        _dbConnection = dbConnection;

    }

    public MyAppContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<MyAppContext>();
        optionsBuilder.UseSqlServer(_dbConnection, opts => opts.CommandTimeout((int)TimeSpan.FromMinutes(15).TotalSeconds));
        return new MyAppContext(optionsBuilder.Options);
    }


}

My Connection string is present in API layer in appsettings.json & as good practice I should not add any reference to EF in API/UI layer.

How do I set the connection string as per environment in such layered architecture?

Thanks.

Author:Kgn-web,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53745874/ef-core-connection-string-cannot-be-null
yy