InvalidOperationException: Error while validating the service descriptor 'ServiceType: BulkyBook.DataAccess.Repository.IRepository.IUnitofWork Lifetime: Scoped ImplementationType: BulkyBook.DataAccess.Repository.UnitofWork': Unable to resolve service for type 'BulkyBook.DataAccess.ApplicationDbContext' while attempting to activate 'BulkyBook.DataAccess.Repository.UnitofWork'.
InvalidOperationException: Unable to resolve service for type 'BulkyBook.DataAccess.ApplicationDbContext' while attempting to activate 'BulkyBook.DataAccess.Repository.UnitofWork'.
This Error Started when I tried to Add Identity Pages in my application by Scaffolding Items
Program.cs
using BulkyBook.DataAccess;
using BulkyBook.DataAccess.Repository;
using BulkyBook.DataAccess.Repository.IRepository;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")
));
builder.Services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddScoped<IUnitofWork, UnitofWork>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{area=Customer}/{controller=Home}/{action=Index}/{id?}");
app.Run();
on line var app = builder.Build
I am facing this error
The code was working file before I added identity razor pages
ApplicationDbContext.cs
using BulkyBook.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace BulkyBook.DataAccess
{
public class ApplicationDbContext: IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :base(options)
{
}
public DbSet<Category> Categories { get; set; }
public DbSet<CoverType> CoverTypes { get; set; }
public DbSet<Product> Products { get; set; }
}
}
UnitofWork.cs
using BulkyBook.DataAccess.Repository.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BulkyBook.DataAccess.Repository
{
public class UnitofWork : IUnitofWork
{
private ApplicationDbContext _db;
public UnitofWork(ApplicationDbContext db)
{
_db = db;
Category = new CategoryRepository(_db);
CoverType = new CoverTypeRepository(_db);
Product = new ProductRepository(_db);
}
public ICategoryRepository Category { get; private set; }
public ICoverTypeRepository CoverType { get; private set; }
public IProductRepository Product { get; private set; }
public void Save()
{
_db.SaveChanges();
}
}
}
This is my first WebApplication using .NET I am using .NET 6.0
I have tried referring to these questions : System.AggregateException: 'Some services are not able to be constructed' In my ASP.net core
and : https://learn.microsoft.com/en-us/answers/questions/527398/some-services-are-not-able-to-be-constructed-dbcon
but not been able to resolve my error
Please do let me know if needed any more code.