In my project I have created this DbContext derived class:
public class SqliteContext : DbContext, IRepoContext
{
private readonly string _fullname;
public SqliteContext()
{
_fullname = SqliteHelpers.CreateDbFullName();
}
public SqliteContext(IConfiguration configuration)
{
string connString = configuration.GetConnectionString("MyConnection");
_fullname = connString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlite($@"Data Source={_fullname}");
}
}
and I have updated the Startup class in this way:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(x => Configuration);
services.AddDbContext<IRepoContext, SqliteContext>();
}
If I execute the "dotnet ef database update" command the default ctor of SqliteContext is used instead of the parametrized ctor.
If I don't use the interface
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(x => Configuration);
services.AddDbContext<SqliteContext>();
}
all works fine, but isn't my case.