我试图找到从 Tracing and Caching Provider Wrappers for Entity Framework with Entity Framework 和 DbContext 中使用 EFCachingProvider 的方法。我使用 EF 4.x DbContext Generator for c# 来生成模型类。我已经添加了对 EFProviderWrapperToolkit、EFCachingProvider、EFTracingProvider 的引用。我还在我的 web.config 文件中进行了更改:
<connectionStrings>
<add name="MyEntities" connectionString="Server=myServer;Database=MyDB;User ID=User;Password=pass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="EFTracingProvider" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<add name="EF Caching Data Provider" invariant="EFCachingProvider" description="Caching Provider Wrapper" type="EFCachingProvider.EFCachingProviderFactory, EFCachingProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
<add name="EF Tracing Data Provider" invariant="EFTracingProvider" description="Tracing Provider Wrapper" type="EFTracingProvider.EFTracingProviderFactory, EFTracingProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
<add name="EF Generic Provider Wrapper" invariant="EFProviderWrapper" description="Generic Provider Wrapper" type="EFProviderWrapperToolkit.EFProviderWrapperFactory, EFProviderWrapperToolkit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
</DbProviderFactories>
</system.data>
但是当应用程序启动时,它会在 EFProviderWrapperToolkit 中的 DbConnectionWrapper 中引发错误:
DbProviderFactory factory = DbProviderFactories.GetFactory(providerInvariantName);
这是从 DbContext 继承的类:
public class MyEntities : DbContext
{
public MyEntities()
: base("MyEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Entity<Product>().ToTable("Product");
modelBuilder.Entity<ProductPhoto>().ToTable("ProductPhoto");
base.OnModelCreating(modelBuilder);
}
public DbSet<Product> Product { get; set; }
public DbSet<ProductPhoto> ProductPhoto { get; set; }
}
我已经阅读了帖子:使用 CODEFIRST 使用跟踪和缓存提供程序包装器,但它并没有解决我的问题。
有谁知道该怎么做?