C#C
C#2y ago
VRose

ASP.NET Core Blazor + EF error

I'm programming an ASP.NET Core Blazor and I'm using Entity Framework to contact the database. Everything works fine except when I use an async function which queries the database. I get the following error:
General error: Invalid operation. The connection is closed.


Although I've set the Microsoft SQL Server connection timeout to none yet this still happens

var department = await PrrojectDBService.GetDepartmentByIdWithRelatedData(selectedDepartmentID);

and here's the code for that function:
public async Task<Project.Models.ProjectDB.Department> GetDepartmentByIdWithRelatedData(int departmentId)
    {
        try
        {
            Console.WriteLine("Starting to fetch department data for department ID: " + departmentId);

            var department = await Context.Departments
                .Include(d => d.DepartmentNotes)
                .Include(d => d.DepartmentInfrastructures)
                .Include(d => d.DeviceInstallationStatuses)
                .FirstOrDefaultAsync(d => d.departmentID == departmentId);

            Console.WriteLine("Successfully retrieved department data for department ID: " + departmentId);

            return department;
        }
        catch (DbException dbEx)
        {
            Console.WriteLine("Database-related error: " + dbEx.Message);
            throw;

        }
        catch (Exception ex)
        {
            Console.WriteLine("General error: " + ex.Message);
            throw; 
        }
    }
Was this page helpful?