0
votes

I have method like the DeleteSettingAbout() after in text, where I am still getting error: "System.InvalidOperationException: A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.".

Code of the method is:

public async Task DeleteSettingAbout(int Id)
    {
        SettingAbout setting = await _context.SettingsAbout.FirstOrDefaultAsync(o => o.Id == Id);

        if (setting != null)
        {
            _context.SettingsAbout.Remove(setting);

            await _context.SaveChangesAsync();
        }
    }

In sartup.cs I set DBContext and DBRepository as Transient:

    services.AddDbContext<AppDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("AppDBConnection")),
            ServiceLifetime.Transient);
        services.AddTransient<IAppDbRepository, SQLAppDbRepository>();

But I am still getting this error.

How to solve this behavior? Thanks for answers.

UPDATE 2021-01-06

I tried the approach with creating the "DbContextFactory" and it solved my problem. I got inspiration from sample app https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/blazor/common/samples/3.x/BlazorServerEFCoreSample (mentioned here: https://docs.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-3.1#sample-app-3x).

Now I have in my startup.cs this:

    // new way suitable for Blazor - register factory and configure the options (new instance for each method call)
        services.AddDbContextFactory<AppDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("AppDBConnection")));
        services.AddScoped<IAppDbRepository, SQLAppDbRepository>();
1
Why don't you upgrade to Net5.0 ! You'll need to implement the dbcontext factory. See the docs: docs.microsoft.com/en-us/aspnet/core/blazor/… - enet
Is there any other code using _context at the same time? - Stephen Cleary
For enet: good notice about the version. But my web-hosting supports only .net core 3.1 for now. I am quite new in Blazor, so thank you for recomendation of dbcontext factory. - Martin Zaloga
For Stephen Cleary: I would say no, because I do not know about other code using the dbcontext at the same time. - Martin Zaloga
For Nijenhof: tommorow I will try it And give you response. Now I am on mobile... - Martin Zaloga

1 Answers

0
votes

I tried the approach with creating the "DbContextFactory" (mentioned by Stephen Cleary) and it solved my problem. I got inspiration from sample app https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/blazor/common/samples/3.x/BlazorServerEFCoreSample (mentioned here: https://docs.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-3.1#sample-app-3x).

Now I have in my startup.cs this:

// new way suitable for Blazor - register factory and configure the options (new instance for each method call)
    services.AddDbContextFactory<AppDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("AppDBConnection")));
    services.AddScoped<IAppDbRepository, SQLAppDbRepository>();

Note: I needed to solve the problem in EF/Blazor = v3.1 (because my web-hosting does not support v5 for now)

Thank you all for answers!