ef core An attempt was made to use the context while it is being configured.
报错信息:
System.InvalidOperationException: An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point. This can happen if a second operation is started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
我在前台同时请求两个后台接口,然后发现经常会出现一个接口正常而另一个接口报错的问题。查询网上资料后,一个解决方法是把Startup文件里配置数据库的 context生命周期改为每次请求都实例化一下。我更改之后,刷新了几次页面发现这个改法可行。不过过了一会儿,我再次刷新页面时,发现原来那个问题又出现了,也就是说问题没有解决。
一次偶然间,我突然发现我的_context 定义的是静态类...我人傻了,改为非静态来之后,可以了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)