I want to run this query:
public IList<User> GetList()
{
using (var context = new MssqlContext())
{
// this throws the exception
var query = from h in context.Users
select h;
var users = query.ToList<User>();
return users;
}
}
But on the marked line is this exception:
System.InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.
The problem might be because I haven't installed all essential libraries or I have badly written my MssqlContext class.
Libraries in my project: https://i.stack.imgur.com/1UrRG.jpg
My question is: What am I missing? This should to all intents and purposes work, but is doesn't.
Similar problem was solved here: 'No database provider has been configured for this DbContext' on SignInManager.PasswordSignInAsync
...but it didn't help me. Thanks you for any suggestions.
Class MssqlContext:
public class MssqlContext : DbContext
{
private readonly string _connectionString;
public DbSet<User> Users { get; set; }
public MssqlContext()
{
// Temporary solution.
var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true);
var config = builder.Build();
//_connectionString = ConfigurationManager;
_connectionString = config["ConnectionStrings:SAVConnectionString"];
}
}