© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
77 replies
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.
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);
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; 
        }
    }
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; 
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

✅ ASP.NET API (EF Core) Relationships
C#CC# / help
4y ago
✅ Seeking Guidance on Migrating ASP.NET MVC to ASP.NET Core MVC and EF to EF Core
C#CC# / help
3y ago
✅ ASP.NET Core MVC error
C#CC# / help
3y ago