DbContext pooling در EF Core 2.0
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۰۶/۱۳ ۱۱:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
services.AddDbContext<BloggingContext>( options => options.UseSqlServer(connectionString));
services.AddDbContextPool<BloggingContext>( options => options.UseSqlServer(connectionString));
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options) : base(options){} public static void RunWithoutContextPooling()
{
Console.WriteLine("\nRun Without ContextPooling");
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkSqlServer()
.AddDbContext<BloggingContext>(
c => c.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Demo.ContextPooling;Trusted_Connection=True;ConnectRetryCount=0;"))
.BuildServiceProvider();
new RunTests().Start(serviceProvider);
} public static void RunWithContextPooling()
{
Console.WriteLine("\nRun With ContextPooling");
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkSqlServer()
.AddDbContextPool<BloggingContext>(
c => c.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Demo.ContextPooling;Trusted_Connection=True;ConnectRetryCount=0;"),
poolSize: 16)
.BuildServiceProvider();
new RunTests().Start(serviceProvider);
} Run Without ContextPooling [10:49:30.728] Context creations: 637 | Requests per second: 597 [10:49:31.746] Context creations: 1069 | Requests per second: 1050 [10:49:32.765] Context creations: 1088 | Requests per second: 1067 [10:49:33.784] Context creations: 1139 | Requests per second: 1119 [10:49:34.802] Context creations: 1138 | Requests per second: 1117 [10:49:35.831] Context creations: 1153 | Requests per second: 1120 [10:49:36.845] Context creations: 1126 | Requests per second: 1111 [10:49:37.873] Context creations: 1014 | Requests per second: 987 [10:49:38.898] Context creations: 1139 | Requests per second: 1111 [10:49:39.918] Context creations: 1086 | Requests per second: 1065 Total context creations: 10592 Requests per second: 1034 Run With ContextPooling [10:49:40.982] Context creations: 32 | Requests per second: 1388 [10:49:41.991] Context creations: 0 | Requests per second: 1691 [10:49:43.014] Context creations: 0 | Requests per second: 1684 [10:49:44.031] Context creations: 0 | Requests per second: 1702 [10:49:45.049] Context creations: 0 | Requests per second: 1694 [10:49:46.067] Context creations: 0 | Requests per second: 1401 [10:49:47.075] Context creations: 0 | Requests per second: 1510 [10:49:48.107] Context creations: 0 | Requests per second: 1669 [10:49:49.127] Context creations: 0 | Requests per second: 1679 [10:49:50.147] Context creations: 0 | Requests per second: 1688 Total context creations: 32 Requests per second: 1610
services.AddEntityFrameworkSqlServer();
services.AddDbContextPool<ApplicationDbContext>((serviceProvider, optionsBuilder) =>
{
optionsBuilder.UseSqlServer("...");
optionsBuilder.UseInternalServiceProvider(serviceProvider);
}); using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; var siteSettings = this.GetService<IOptionsSnapshot<SiteSettings>>(); // Or var siteSettings = this.GetInfrastructure().GetRequiredService<IOptionsSnapshot<SiteSettings>>();