✅ error

this is error
17 Replies
mer_nam hai bulla rakhta hu kula
this is applicationDb file
using Microsoft.EntityFrameworkCore;
using Office.DataAccess;
using Office.DataAccess.Repository;
using Office.DataAccess.Repository.IRepository;
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")
));
//options => options.SignIn.RequireConfirmedAccount = true
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: "{controller=Home}/{action=Index}/{id?}");

app.Run();
using Microsoft.EntityFrameworkCore;
using Office.DataAccess;
using Office.DataAccess.Repository;
using Office.DataAccess.Repository.IRepository;
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")
));
//options => options.SignIn.RequireConfirmedAccount = true
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: "{controller=Home}/{action=Index}/{id?}");

app.Run();
this is program.cs file
Tvde1
Tvde13y ago
as I understand it, it cannot create a UnitOfWork because it does not know how to make an Office.DataAccess.ApplicationDbContext but you do register the context successfully the ApplicationDbContext added to the services in Program.cs is 100% Office.DataAccess.ApplicationDbContext?
Tvde1
Tvde13y ago
delete /bin and /obj and run again? 😬 And where is it trying to create a IUnitOfWork?
mer_nam hai bulla rakhta hu kula
here it is invoking applicationDbcontext in unitofwork
Tvde1
Tvde13y ago
yeah but usually the error would also show that e.g. a controller requires IUnitOfWork
mer_nam hai bulla rakhta hu kula
here this is the project file see if you can solve
V0FBU1VM
V0FBU1VM3y ago
Your dbcontext seems to inherit from IdentityDbContent. But you haven't specified the IdentityUser. Try this
public class ApplicationDbContext: IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
// Add your DbSet's
}
public class ApplicationDbContext: IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
// Add your DbSet's
}
Just a tips, don't use UnitOfWork. It's already kindof implemented in ef. Also don't make your dbsets nullable If you don't like the warning that it gives do something like this
public DbSet<Emp> Employees => Set<Emp>();
public DbSet<Emp> Employees => Set<Emp>();
Tvde1
Tvde13y ago
yeah building a repository atop of EF while still exposing its SaveChanges or IQueryable<T> is a pretty bad design pattern. It abstracts nothing and only limits the use of EF
V0FBU1VM
V0FBU1VM3y ago
Definitely!
V0FBU1VM
V0FBU1VM3y ago
@mer_nam hai bulla rakhta hu kula Have a look at this: https://github.com/WaadSulaiman/SocialMedia.Api
GitHub
GitHub - WaadSulaiman/SocialMedia.Api: A social media api using the...
A social media api using the onion architecture. You can interact with other users by posting, commenting, following and messaging. - GitHub - WaadSulaiman/SocialMedia.Api: A social media api using...
mer_nam hai bulla rakhta hu kula
@SwaggerLife @Tvde1 thanks for input guys but this error is solved+ after posting this question in multiple servers for the error everyone discouraged me for those repository implementation next time I will keep this in mind
Tvde1
Tvde13y ago
it's still good to practice and write code :)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Closed!
Want results from more Discord servers?
Add your server