Entity Framework 6二级缓存,转码为vb.net
Entity Framework 6 second level cache, convert code to vb.net
当我使用 entity framework 从数据中执行大量导入以放入 sql 时,我的代码必须检查导致对 sql 服务器重复命令的相同外键.我相信我需要使用二级缓存,我尝试使用 efcache from nuget,但由于我只习惯 vb.net,我在转换以下 C# 代码时遇到了问题:
public class Configuration : DbConfiguration
{
public Configuration()
{
var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
AddInterceptor(transactionHandler);
Loaded +=
(sender, args) => args.ReplaceService<DbProviderServices>(
(s, _) => new CachingProviderServices(s, transactionHandler,
new DefaultCachingPolicy()));
}
}
到vb
Imports System.Data.Entity
Imports EFCache
Public Class Configuration
Inherits DbConfiguration
Public Sub New()
Dim transactionHandler = New CacheTransactionHandler(New InMemoryCache())
AddInterceptor(transactionHandler)
AddHandler Loaded, Function(sender, args) args.ReplaceService(Of
DbProviderServices)(Function(s, _) New CachingProviderServices(s,
transactionHandler, New DefaultCachingPolicy()))
End Sub
End Class
我知道我需要将 c# += 更改为 addhandler,但它无法识别 dbproviderservices、下划线 _ 和 DefaultCachingPolicy。
您使用的方法签名不正确。
-
Public Shared Event Loaded As EventHandler(Of DbConfigurationLoadedEventArgs)
DbConfigurationLoadedEventArgs.ReplaceService(Of TService)
Public Sub ReplaceService(Of TService) (serviceInterceptor As Func(Of TService, Object, TService))
所以代码应该更像这样:
AddHandler Loaded,
Sub(sender As Object, e As DbConfigurationLoadedEventArgs)
e.ReplaceService(Of DbProviderServices)(
Function(serviceInterceptor As DbProviderServices, o As Object)
Return New CachingProviderServices(serviceInterceptor, transactionHandler, New DefaultCachingPolicy())
End Function)
End Sub
精简版:
AddHandler Loaded, Sub(sender As Object, e As DbConfigurationLoadedEventArgs) e.ReplaceService(Of DbProviderServices)(Function(serviceInterceptor As DbProviderServices, o As Object) New CachingProviderServices(serviceInterceptor, transactionHandler, New DefaultCachingPolicy()))
当我使用 entity framework 从数据中执行大量导入以放入 sql 时,我的代码必须检查导致对 sql 服务器重复命令的相同外键.我相信我需要使用二级缓存,我尝试使用 efcache from nuget,但由于我只习惯 vb.net,我在转换以下 C# 代码时遇到了问题:
public class Configuration : DbConfiguration
{
public Configuration()
{
var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
AddInterceptor(transactionHandler);
Loaded +=
(sender, args) => args.ReplaceService<DbProviderServices>(
(s, _) => new CachingProviderServices(s, transactionHandler,
new DefaultCachingPolicy()));
}
}
到vb
Imports System.Data.Entity
Imports EFCache
Public Class Configuration
Inherits DbConfiguration
Public Sub New()
Dim transactionHandler = New CacheTransactionHandler(New InMemoryCache())
AddInterceptor(transactionHandler)
AddHandler Loaded, Function(sender, args) args.ReplaceService(Of
DbProviderServices)(Function(s, _) New CachingProviderServices(s,
transactionHandler, New DefaultCachingPolicy()))
End Sub
End Class
我知道我需要将 c# += 更改为 addhandler,但它无法识别 dbproviderservices、下划线 _ 和 DefaultCachingPolicy。
您使用的方法签名不正确。
-
Public Shared Event Loaded As EventHandler(Of DbConfigurationLoadedEventArgs)
DbConfigurationLoadedEventArgs.ReplaceService(Of TService)
Public Sub ReplaceService(Of TService) (serviceInterceptor As Func(Of TService, Object, TService))
所以代码应该更像这样:
AddHandler Loaded,
Sub(sender As Object, e As DbConfigurationLoadedEventArgs)
e.ReplaceService(Of DbProviderServices)(
Function(serviceInterceptor As DbProviderServices, o As Object)
Return New CachingProviderServices(serviceInterceptor, transactionHandler, New DefaultCachingPolicy())
End Function)
End Sub
精简版:
AddHandler Loaded, Sub(sender As Object, e As DbConfigurationLoadedEventArgs) e.ReplaceService(Of DbProviderServices)(Function(serviceInterceptor As DbProviderServices, o As Object) New CachingProviderServices(serviceInterceptor, transactionHandler, New DefaultCachingPolicy()))