Loading

DbContextOptions<TDbContext> 的生命周期

public static IServiceCollection AddDbContext<TContextService, TContextImplementation>([JetBrains.Annotations.NotNull] this IServiceCollection serviceCollection, [JetBrains.Annotations.CanBeNull] Action<IServiceProvider, DbContextOptionsBuilder> optionsAction, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContextImplementation : DbContext, TContextService
services.AddDbContext<XXDbContext>(options =>
            {
                options.UseSqlServer(configuration.GetConnectionString("ConnectionString"), sqlOptions =>
                {
                    sqlOptions.MigrationsAssembly(migrationsAssemblyName);
                    sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
                });
            }, ServiceLifetime.Scoped, ServiceLifetime.Singleton);

第一个Lifetime 是确定 XXDbContext的生命周期,默认是ServiceLifetime.Scoped

第二个Lifetime 是确定DbContextOptions<TDbContext> 的生命周期,默认是ServiceLifetime.Scoped

posted @ 2019-11-28 00:59  microestc  阅读(1475)  评论(0编辑  收藏  举报