Hi

I have a .Net Core Blazor server project and I'm making use of the Repository pattern for data access. Initially, I had registered the repository as a scoped service.

While testing I noticed that if I quickly switched pages for e.g. by clicking on the navigation menu, while the repository is being used on the current page, the application would throw an exception related to the DbContext being accessed concurrently.

What's the best way to prevent an issue like this from occurring, besides registering the repository as a transient service, which would mean multiple DbContext instances, I'm currently using this approach as a patch.

Thanks in advance! 😃

You mention data access but didn't mention much about the datastore. For what apps I've developed I use SQL (don't make me list all I've used over the years) and this seems to solve the concurrency issues since the data commit is done by the SQL server before it processes the next query. But hey, that is what I'm seeing. I can imagine if you rolled your own database you get to wrestle concurrency issues.

I'm making use of Microsoft SQL Server as a data store. It seems the concurrency issue you're referring to and the one I'm referring to are not the same. This error message may clarify the concurrency issue I'm referring to:

A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.