C#C
C#3y ago
xdd

✅ DB context injection not working

hi, i have app like this -
DotNetEnv.Env.Load("../build/");
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    WebRootPath = "Static"
});

builder.Services.AddMvc();

builder.Services.AddDbContext<MainContext>();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => options.LoginPath = "/login");

var app = builder.Build();

app.UseDefaultFiles();
app.UseStaticFiles();

app.MapControllers();

app.Run();


and when i use MainContext in endpoint as argmuent it says - (screenshot).

example of endpoint

[Route("api/{genre}/{id}")]
public IActionResult IndexGenre(MainContext db, string keyword, int id)
{
    if (keyword == "default")
    {
        Console.WriteLine("1");
        var result = db.Products.Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList();
        return Json(result);
    }
    else if (keyword.Contains("#"))
    {
        string genre = keyword.Substring(0, keyword.Length - 1);
        var result = db.Products.Where(p => p.Type.Contains(genre)).Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList(); ;
        return Json(result);
    }
    else
    {
        var result = db.Products.Where(p => p.Title.Contains(keyword) || p.Description.Contains(keyword)).Skip(id * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList(); ;
        return Json(result);
    }
}
image.png
Was this page helpful?