Open Closed

How to add interceptor to EF Core db context #1430


User avatar
0
Denis created

I need t configure DB context to use this library https://github.com/VahidN/EFCoreSecondLevelCacheInterceptor

How I can do that in ABP?

The code below is used to enable this interceptor

public static class MsSqlServiceCollectionExtensions
    {
        public static IServiceCollection AddConfiguredMsSqlDbContext(this IServiceCollection services, string connectionString)
        {
            services.AddDbContextPool<ApplicationDbContext>((serviceProvider, optionsBuilder) =>
                    optionsBuilder
                        .UseSqlServer(
                            connectionString,
                            sqlServerOptionsBuilder =>
                            {
                                sqlServerOptionsBuilder
                                    .CommandTimeout((int)TimeSpan.FromMinutes(3).TotalSeconds)
                                    .EnableRetryOnFailure()
                                    .MigrationsAssembly(typeof(MsSqlServiceCollectionExtensions).Assembly.FullName);
                            })
                        .AddInterceptors(serviceProvider.GetRequiredService<SecondLevelCacheInterceptor>()));
            return services;
        }
    }

ABP Version: 4.3


2 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Open EntityFrameworkCoreModule class and remove Configure<AbpDbContextOptions>... code block.

    Open EntityFrameworkCoreDbMigrationsModule clsss and add following code:

    
    context.Services.AddEFSecondLevelCache(options =>
        options.UseMemoryCacheProvider().DisableLogging(true).UseCacheKeyPrefix("EF_"));
        
    Configure<AbpDbContextOptions>(options =>
    {
        options.Configure(c =>
        {
            c.UseSqlServer(builder =>
            {
                builder.CommandTimeout((int) TimeSpan.FromMinutes(3).TotalSeconds)
                    .MigrationsAssembly(typeof(QaEntityFrameworkCoreDbMigrationsModule).Assembly.FullName);
            }).AddInterceptors(c.ServiceProvider.GetRequiredService<SecondLevelCacheInterceptor>());
        });
    });
    
  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09