Вот какая ошибка:
Невозможно получить доступ к удаленному экземпляру контекста. Распространенной причиной этой ошибки является удаление экземпляра контекста, который был разрешен в результате внедрения зависимостей, а затем попытка использовать тот же экземпляр контекста в другом месте вашего приложения. Это может произойти, если вы вызываете Dispose для экземпляра контекста или заключаете его в оператор using. Если вы используете внедрение зависимостей, вы должны позволить контейнеру внедрения зависимостей позаботиться об удалении экземпляров контекста.
Имя объекта: '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(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("BKRConnection"),
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.EnableRetryOnFailure();
});
});
builder.Services.AddScoped();
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().DisableAntiforgery().AddInteractiveServerRenderMode();
app.Run();
Код: Выделить всё
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 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();//
Подробнее здесь: [url]https://stackoverflow.com/questions/78824578/ef-core-8-cannot-access-a-disposed-context-instance[/url]