Error Discription
System.ObjectDisposedException: 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: 'MyDbContext'.
My Model Class
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SAPConnection.Data
{
public class LeaveModel
{
[Key]
public int Id { get; set; }
public String Reason { get; set; } = "";
public DateTime RequestDate { get; set; } = DateTime.Now;
public DateTime ToDate { get; set; } = DateTime.Now;
public DateTime FromDate { get; set; } = DateTime.Now;
public LeaveTypeModel LeaveType { get; set; }
[NotMapped]
public byte[] Attachment { get; set; }
}
}
public enum LeaveTypeModel
{
Casual, Sick, Annual
}
My LeaveService Class
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SAPConnection.Data
{
public class LeaveService
{
private readonly MyDbContext _context;
public LeaveService(MyDbContext context)
{
_context = context;
}
public async Task<List<LeaveModel>> GetLeavesAsync()
{
return await _context.leaveModel.ToListAsync();
}
public async Task<LeaveModel> GetLeaveAsync(int id)
{
return await _context.leaveModel.FindAsync(id);
}
public async Task CreateLeaveAsync(LeaveModel leave)
{
_context.leaveModel.Add(leave);
await _context.SaveChangesAsync();
}
public async Task UpdateLeaveAsync(LeaveModel leave)
{
_context.Entry(leave).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
public async Task DeleteLeaveAsync(int id)
{
var leave = await _context.leaveModel.FindAsync(id);
_context.leaveModel.Remove(leave);
await _context.SaveChangesAsync();
}
}
}
My Function which submit leaves
private async Task submitLeaves()
{
await leaveService.CreateLeaveAsync(newLeave);
NavigationManager.NavigateTo("/");
}
My Program.cs Class
using Blazored.Toast;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using SAPConnection.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using SAPConnection.Areas.Identity.Data;
using System.Configuration;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("SAPConnectionContextConnection") ?? throw new InvalidOperationException("Connection string 'SAPConnectionContextConnection' not found.");
builder.Services.AddDbContext<SAPConnectionContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<SAPConnectionContext>();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddScoped<LeaveService>();
builder.Services.AddBlazoredToast();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// 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.UseAuthentication();
app.UseAuthorization();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();