当我从数据执行大量导入以使用实体框架放入sql时,我的代码必须检查导致sql server重复命令的相同外键。我相信我需要使用二级缓存,我尝试使用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。
答案 0 :(得分:1)
您使用的方法签名不正确。
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()))