惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - 王育东

Redis + Shiro + FastJson@Cacheable无法写入缓存 canal-client无法获取数据 mysql5.7设置默认编码 Angular No name was provided for external module 'XXX' in output.globals 错误 Angular cli 发布自定义组件 Windows Message ID 常量列表大全 C#中Thread与ThreadPool的比较 HTML元素隐藏和显示 IM服务器架构实现 TCP打洞技术 C# UDP打洞 COM ActiveX C++ Builder 十分经典的批处理教程 MVVM-Light模式,在dataGrid的模板下,绑定事件不触发的原因已经服务端排序的实现 ADO.NET Entity Framework 如何输出日志到 log4net (EF, Log4net) Silverlight 发布测试 WCF发布到IIS7问题的解决方案 慢慢的才知道 Javascript在页面加载时的执行顺序 - 王育东 - 博客园
Entity Framework 4 CodeFirst EFProviderWrapperToolKit 使用
王育东 · 2012-06-08 · via 博客园 - 王育东

EF团队推出了一套比较完整的缓存和SQL执行日志的解决方案,EFProviderWrappers。他们的做法是在原来的EF Provider之上,再加一层包装,通过这层包装拦截,进行数据缓存和日志监控。数据缓存功能与NHibernate的二级缓存相比,优势在于简单轻量。

最近在做的一个项目采用了EntityFramework4.3 Code First 模式开发,希望引入这个组件完成日志跟踪和缓存功能。

在网上搜寻EFProviderWrappers 相关资料基本上都是基于ObjectContext而不是DbContext。而且这个组件也没有更新,最新版本也是2011年4月。

有什么方法才能在EF4上正常使用呢?

现将研究的方法总结一下

  1.  首先是下载这套组件EFProviderWrapperToolKit
  2. 在项目中引用EFProviderWrapperToolkit.dll、EFTracingProvider.dll和  EFCachingProvider.dll 
  3. 在Web.Config 或App.Config配置文件中增加配置节

<connectionStrings>                                            

     <add name="TestContext"          

             providerName="System.Data.SqlClient"          

             connectionString="Server=Wyd\Instance;Initial Catalog=DatabaseName;User ID=sa;Password=123;MultipleActiveResultSets=True"/>

</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>

4.在DbContext类中 建立两个构造方法(这就是关键处了)

 1 public class TestContext : DbContext
 2 {
 3    public TestContext() 
 4    { 
 5    }   
 6    public TestContext(DbConnection conn)
 7         :Base(conn,True) 
 8    { 
 9    }
10 } 

 5.  OverLoad the DbConnection.

1 using (var context = new TextContext(CreateConnectionWrapper(@"name=TestContext"))) 
2 {                  
3     var product = context.ProductCollection.Find(1);             
4 }

6. 添加CreateConnectionWrapper 方法

private static DbConnection CreateConnectionWrapper(string nameOrConnectionString) 
{     
   var providerInvariantName ="System.Data.SqlClient";     
   var connectionString = nameOrConnectionString;     //name=connectionName format     
   var index = nameOrConnectionString.IndexOf('=');     
   if (nameOrConnectionString.Substring(0,index).Trim().Equals("name", StringComparison.OrdinalIgnoreCase))     
   {
       nameOrConnectionString=nameOrConnectionString.Substring(index + 1).Trim();     
   }     
   //look up connection string name     
   var connectionStringSetting = ConfigurationManager.ConnectionStrings[nameOrConnectionString];     
   if (connectionStringSetting != null)     
   {         
       providerInvariantName = connectionStringSetting.ProviderName;
             connectionString = connectionStringSetting.ConnectionString;     
      }     
      //create the special connection string with the provider name in it     
      var wrappedConnectionString = wrappedProvider=" + providerInvariantName + ";" + connectionString;     
      //create the tracing wrapper     
      var connection = new EFTracingConnection                            
      {                                    
           ConnectionString = wrappedConnectionString                              };     
      //写日志logging

      connection.CommandFinished += (sender, args) => Console.WriteLine(args.ToTraceString());    

      return connection;