项目场景:
在使用.net core开发时,经常使用数据库出现的问题。
问题描述
开发ASP.NET Core时遇到在经常使用数据库连接时报错误提示:
Unable to cast object of type'System.Data.ProviderBase.DbConnectionClosedConnecting' to type 'System.Data.SqlClient.SqlInternalConnectionTds'.
原因分析:
这是由.net core 并发下注入模式引起的线程安全问题。
解决方案:
查看Startup.cs文件下,IServiceCollection services对象是否使用的services.AddSingleton()单例方式,将AddSingleton()改为services.AddScoped();问题解决。