I am using dependency injection to initiate my db context in my blazor web app (serverside)
Below is my code and error message
Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'BKRContext'.
using BKR.DB;
using BKR.Web.Components;
using BKR.WEB.Services;
using BKR.WEB.Services.Contracts;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddDbContextPool<BKRContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("BKRConnection"),
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.EnableRetryOnFailure();
});
});
builder.Services.AddScoped<ICustomerService, CustomerService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// 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.UseAntiforgery();
app.MapRazorComponents<App>().DisableAntiforgery().AddInteractiveServerRenderMode();
app.Run();
Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling ‘Dispose’ on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: ‘BKRContext’.
using BKR.Model;
using BKR.WEB.Services.Contracts;
using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.AspNetCore.WebUtilities;
using BKR.DB;
using Microsoft.EntityFrameworkCore;
namespace BKR.WEB.Services
{
public class CustomerService : ICustomerService
{
private readonly BKRContext dbcontext;
public CustomerService(BKRContext bkrdbcontext)
{
this.dbcontext = bkrdbcontext;
}
public async ValueTask<BKR.Model.Customer> CreateCustomer(BKR.Model.Customer dto)
{
BKR.Model.Customer bolObj = new BKR.Model.Customer()
try
{
var result = await this.dbcontext.Customers.AddAsync(bolObj);
await this.dbcontext.SaveChangesAsync();// <<-- Error here
return result.Entity;
}
catch (Exception ex)
{
throw;
}
}
using BKR.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace BKR.DB;
public class BKRContext : DbContext
{
//public BKRContext()
//{
//}
public BKRContext(DbContextOptions<BKRContext> dbContextOptions)
: base(dbContextOptions)
{
//Database.EnsureCreated();
}
//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//{
// IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile("appsettings.json").Build();
// optionsBuilder.UseSqlServer(configuration.GetConnectionString("BKRConnection"));
//}
}
Any ideas why?
Thanks in advance