Can MS provide guidance and code to Avoiding DbContext threading issues when using Blazor server-side?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
I'm having problems here too. The documented guidance is not enough. The error happens anyway.
Not sure if this will help, but adding the code below fixed this issue for me.
'services.AddDbContext
The struggle for me at least, was sharing a dbcontext with more than 1 blazor component per page. A component and its subcomponent (and I assume other subcomponents) will run OnInitializedAsync() in parallel, leading to a concurrency error if they share the same dbcontext. Best practice then, is to design your components so they can use a separate dbcontext.
This is definitely an issue. With this still fresh in my mind, the only solutions I have come up with so far was to join everything into 1 supercomponent, or to use different dbcontexts but then orchestrate a save among them all.
I will be looking further into how to minimize the "editing" dbcontext, and using components that work with self-contained dbcontexts (and don't need to save). Definitely a big change in process, coming from Silverlight where it was fine to load everything into one great big dbcontext...
@JeremyLikness I think we can close this now, right?
@ajcvickers In my opinion, yes.