C# This is usually caused by different threads concurrently using the same instance of DbContext

574 Views Asked by At

I am getting an error in the system, what is the reason? I wasn't getting such errors in .NET 5.

Exception location

Exception text

I've never encountered such a problem in APIs, I'm actually pretty new to async methods, can someone enlighten me :) I translated AsQueryable to filter later

private DataContext _Context;
private IFileUpload _File;

public ProductRepository(DataContext Context, IFileUpload file) : base(Context)
{
    _Context = Context;
    _File = file;
}

public async Task<Product> GetProductByUrl(string ProductUrl, bool tracking = false)
{
    var Model = _Context.Products
        .Include(i => i.ProductPictures)
        .Include(i => i.MyProperty)
        .ThenInclude(i => i.Category)
        .AsQueryable();
    if (!tracking)
    {
        Model = Model.AsNoTracking();
    }
    return await Model.FirstOrDefaultAsync(i => i.Url == ProductUrl);
}
0

There are 0 best solutions below