Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Cannot access a disposed context instance in Blazor Web App

$
0
0

I am getting an error

Cannot access a disposed context instance...

when trying to execute two calls to the database - one right after the other. This is a Blazor web app using SQL Server.

The code in Program.cs:

builder.Services.AddDbContext<MyDbContext>(options =>     options.UseSqlServer(builder.Configuration.GetConnectionString("Laptop")));builder.Services.AddTransient<IEquipmentRepository, EquipmentRepository>();

The code calling the repository placed in OnInitializeAsync:

CalibrationTypes = await EqRepo.GetAllEquipmentCalibrationTypes();Calibrations = await EqRepo.GetAllCalibrationsForEquipment(Equipment.ID);

The code for GetAllEquipmentCalibrationTypes:

List<EquipmentCalibrationType> equipmentCalibrationTypes = await context.EquipmentCalibrationTypes.ToListAsync();return equipmentCalibrationTypes;

The code for GetAllCalibrationsForEquipment:

equipmentCalibrations = await context.EquipmentCalibrations.Where(ec => ec.Equipment.ID == equipmentID).ToListAsync();return equipmentCalibrations;

I have other code elsewhere that makes multiple calls to the repos and have not had the DbContext disposed. Not sure where or what is disposing the DbContext?

I have reviewed a few other questions on SO and they all say to structure the DbContext as I have and make sure to await the processes. The methods are all marked as async.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>