I am working on project with no repository pattern. I am trying to seperate my business logic from controllers, kind of setting up a business layer. I do the following to get all of my users.
DbContext
public class DatabaseContext : DbContext
{
public DatabaseContext() : base() { }
public DatabaseContext(DbContextOptions options) : base(options) { }
public DbSet<User> Users { get; set; }
public DbSet<OvertimeRequest> OvertimeRequests { get; set; }
public DbSet<HolidayRequest> HolidayRequests { get; set; }
public DbSet<PaymentRequest> PaymentRequests { get; set; }
}
OvertimeRequestBusiness
public class OvertimeRequestBusiness
{
public static OvertimeRequestBusiness Instance { get; } = new
OvertimeRequestBusiness();
public OvertimeRequestBusiness() { }
public async Task<List<User>> GetAllUsersAsync()
{
using (var ctx = new DatabaseContext())
{
var query = ctx.Users;
var res = await query.ToListAsync();
return res;
}
}
}
Controller
[Route("users"), HttpGet]
public async Task<List<User>> GetAllUsers()
{
return await OvertimeRequestBusiness.Instance.GetAllUsersAsync();
}
And the error I get is
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<TContext> object in its constructor and passes it to the base constructor for DbContext**