EFCore2.0 為DbContext引入新的注冊方式:透明地注冊了 DbContext實例池
services.AddDbContextPool<UserModelContext>(options => options.UseSqlServer(Configuration.GetConnectionString("sqlstring")));
一如既往支持lambda方式注冊連接字符串
- 默認的連接池數量為 128
- 每次使用完DbContext不會釋放對象,而是重置並回收到DBContextPool
Web程序中通過重用池中DbContext實例可提高高並發場景下的吞吐量, 這在概念上類似於ADO.NET Provider原生的連接池操作方式,具有節省DbContext實例化成本的優點, 這也是EFCore2.0 其中一個性能亮點。
驗證SQL Server會話中的有效連接數SQL:
SELECT DEC.session_id, DEC.protocol_type, DEC.auth_scheme, DES.login_name, DES.login_time FROM sys.dm_exec_sessions AS DES JOIN sys.dm_exec_connections AS DEC ON DEC.session_id = DES.session_id;