SteffenMangold / EntityFrameworkCore.Cacheable

EntityFrameworkCore second level cache
Apache License 2.0
192 stars 27 forks source link

Support MySQL #5

Closed geffzhang closed 5 years ago

geffzhang commented 5 years ago

Describe what is not working as expected.

If you are seeing an exception, include the full exceptions details (message and stack trace).

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at Wilmar.Wmp.Gateway.Application.WmpGatewayApplicationModule.OnApplicationInitialization(ApplicationInitializationContext context) in C:\Users\zhangshanyouv\source\Workspaces\KDS X1.0\02-代码库\Framework\VNext\wmp\modules\gateways\src\Wilmar.Wmp.Gateway.Application\WmpGatewayApplicationModule.cs:line 41
   at Volo.Abp.Modularity.ModuleManager.InitializeModules(ApplicationInitializationContext context)
   at Volo.Abp.AbpApplicationBase.InitializeModules()
   at Wilmar.Wmp.Gateway.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime applicationLifetime) in C:\Users\zhangshanyouv\source\Workspaces\KDS X1.0\02-代码库\Framework\VNext\wmp\modules\gateways\src\Wilmar.Wmp.Gateway\Startup.cs:line 34

内部异常 1:
NotSupportedException: Could not parse expression 'value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Wilmar.Wmp.Gateway.Domain.WmpGlobalConfiguration]).Include(x => x.WmpReRoutes).Where(x => (x.IsDefault == __isDefault_0)).Cacheable(value(EntityFrameworkCore.Cacheable.CacheableOptions))': This overload of the method 'EntityFrameworkCore.Cacheable.EntityFrameworkQueryableExtensions.Cacheable' is currently not supported.

Steps to reproduce

Use MySQL

private void ConfigureDatabaseServices(IServiceCollection services, IConfigurationRoot configuration)
        {
            services.Configure<DbConnectionOptions>(options =>
            {
                options.ConnectionStrings["WmpGateway"] = configuration.GetConnectionString("WmpGateway");
                options.ConnectionStrings.Default = configuration.GetConnectionString("Default");
            });

            services.Configure<AbpDbContextOptions>(options =>
            {
                options.UseMySQL();
            });
        }

WmpGatewayDbContext:
       protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSecondLevelCache();
            }
        }

       public async Task<WmpGlobalConfiguration> GetGlobalConfiguration(int isDefault = 1)
        {             
            var cacheableQuery = DbSet.Include(x=> x.WmpReRoutes)
                .Where(x => x.IsDefault == isDefault)
                .Cacheable(TimeSpan.FromSeconds(60));
            return await cacheableQuery.FirstOrDefaultAsync();
        }

Further technical details

EntityFrameworkCore.Cacheable version: 2.0.0 EF Core version: 2.2 IDE: Visual Studio 2017 15.9

SteffenMangold commented 5 years ago

HI,

could you put in a break here:

if (!optionsBuilder.IsConfigured)
{
    optionsBuilder.UseSecondLevelCache(); // <---- here
}

It looks like UseSecondLevelCache is never been called.

SteffenMangold commented 5 years ago

In your sample, you have to change the position of UseSecondLevelCache.

[...]
    services.Configure<AbpDbContextOptions>(options =>
    {
        options.UseMySQL();
                options.UseSecondLevelCache() // add here for setup
    });
}

WmpGatewayDbContext:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
                // this would never be called because context is already configured
        optionsBuilder.UseSecondLevelCache(); // can be removed
    }
}