![]() ![]() Since the tenant ID needs to change with each web request, we need to go through some extra steps to make it all work with context pooling. a tenant ID).Ī typical scenario involving context state would be a multi-tenant ASP.NET Core application, where the context instance has a tenant ID which is taken into account by queries (see Global Query Filters for more details). Crucially, the context's OnConfiguring is only invoked once - when the instance context is first created - and so cannot be used to set state which needs to vary (e.g. This means that special care must be taken when the context involves any state that may change between requests. MethodĬontext pooling works by reusing the same context instance across requests this means that it's effectively registered as a Singleton, and the same instance is reused across multiple requests (or DI scopes). The source code is available here, feel free to use it as a basis for your own measurements. Importantly, this benchmarks single-threaded pooling performance, while a real-world contended scenario may have different results benchmark on your platform before making any decisions. As always, results will change with the number of rows, the latency to your database server and other factors. Benchmarksįollowing are the benchmark results for fetching a single row from a SQL Server database running locally on the same machine, with and without context pooling. Once poolSize is exceeded, new context instances are not cached and EF falls back to the non-pooling behavior of creating instances on demand. The poolSize parameter of the PooledDbContextFactory constructor sets the maximum number of instances retained by the pool (defaults to 1024). Using (var context = factory.CreateDbContext()) To use context pooling without dependency injection, initialize a PooledDbContextFactory and request context instances from it: var options = new factory = new PooledDbContextFactory(options) The poolSize parameter of AddDbContextPool sets the maximum number of instances retained by the pool (defaults to 1024). O => o.UseSqlServer(("WeatherForecastContext"))) To enable context pooling, simply replace AddDbContext with AddDbContextPool: ( Then, instances of that type are obtained through constructor parameters in controllers or Razor Pages. Note that I also use a different connection string during migrations - the user has higher permissions than the website and the connection timeout is longer.The typical pattern in an ASP.NET Core app using EF Core involves registering a custom DbContext type into the dependency injection container via AddDbContext. ![]() Migrations: timeout error in Update-Database commands DbMigrationsConfiguration.CommandTimeout Property Internal sealed class Configuration : DbMigrationsConfigurationĪutomaticMigrationDataLossAllowed = false ĬommandTimeout = 360 // <- 6 minute timeout! My Migrations.Configuration class: using Using (var context = new M圜ontext(Config.ConnectionStringMigrations))ĭatabase.SetInitializer(new MigrateDatabaseToLatestVersion()) My DatabaseMigrationConfig Class: public class DatabaseMigrationConfig In my : protected void Application_Start() Using Entity Framework 6 (NOT CORE!), I set a longer timeout for migrations using the DbMigrationsConfiguration.CommandTimeout property. But you can set it globally in the constructor, and then remove it later if you don't need to keep it: public class ApplicationDbContext : DbContext There is no way to set the CommandTimeout on a context when using EF commands. When the tooling runs the migration, it looks first for a class that implements IDesignTimeDbContextFactory and if found, will use that for configuring the context. Make sure that your existing DbContext has a constructor that takes a DbContextOptions object as a parameter: public AdventureContext(DbContextOptions options) : base(options) Return new SampleContext(optionsBuilder.Options) Var optionsBuilder = new => opts.CommandTimeout((int)TimeSpan.FromMinutes(10).TotalSeconds)) Public SampleContext CreateDbContext(string args) Public class SampleContextFactory : IDesignTimeDbContextFactory The error message you are getting is for a Command timeout, not a connection timeout.Īs mentioned by Pace in comments, since EF Core 2.0 you are able to use IDesignTimeDbContextFactory to change the behaviour of your context when it is being created by tooling at design time such as happens with Migrations.Ĭreate a separate class in your project that implements the IDesignTimeDbContextFactory interface and use the DbContextoptionsBuilder to configure the behaviour you want - in this case, setting the command timeout value to 600 seconds: using Microsoft.EntityFrameworkCore ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |