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

推荐订阅源

IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
雷峰网
雷峰网
罗磊的独立博客
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
P
Proofpoint News Feed
The Cloudflare Blog
D
Docker
大猫的无限游戏
大猫的无限游戏

博客园 - Bruse

【收藏】Windows 8 Consumer Preview的108个运行命令及简要说明 【转载】菜鸟进阶达人攻略 苹果iPad使用小技巧 Razor语法总结 省空间,教你将C盘iTunes的IPhone备份文件完整的移到其他盘 【翻译】The Repository Pattern with EF code first & Dependeny Injection in ASP.NET MVC3 【原创】越狱以后的iPhone从4.3.3升级到5.0.1时遇到的一些问题的解决办法总结 【转载】SQL Server 中几个有用的特殊函数 T-SQL查询进阶--流程控制语句 【转载】ASP.NET 尖括号 百分号 井号 等号 的用法 【转载】生活常识,人人必备 【转载】ASP.Net程序开发性能优化---数据库优化 关于IIS在使用中遇到的一些问题的总结 【转载】关于IPhone/IPad升级的那点事 【转载】MySql数据备份方案 【转载】MySql中如何限制日志文件的无限制扩张 【转载】mysql5.1免安装的使用及其配置说明 Apple越狱后超级实用的Cydia常用源推荐 MySQL数据库备份与恢复的详细说明 分享45个海量免费电子书下载网站
EF中DataContext创建的两段代码收藏
Bruse · 2011-09-09 · via 博客园 - Bruse

DataContextAutoDisposeModule

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Collections;
 6 using System.Data.Linq;
 7 
 8 /// <summary>
 9 /// 实现自动抛弃当前数据库上下文的模块。
10 /// </summary>
11 public sealed class DataContextAutoDisposeModule : IHttpModule
12 {
13     #region IHttpModule 成员
14     public void Init(HttpApplication context)
15     {
16         context.PostRequestHandlerExecute += new EventHandler(Enter);
17     }
18 
19     public void Dispose()
20     {
21     }
22     #endregion
23 
24     private void Enter(object sender, EventArgs e)
25     {
26         IDictionary httpContextItems = HttpContext.Current.Items;
27         List<object> keys = new List<object>(httpContextItems.Count);
28 
29         foreach (DictionaryEntry item in httpContextItems)
30         {
31             DataContext dataContext = item.Value as DataContext;
32             if (dataContext != null)
33             {
34                 dataContext.Dispose();
35                 keys.Add(item.Key);
36             }
37         }
38 
39         foreach (object key in keys)
40         {
41             httpContextItems.Remove(key);
42         }
43     }
44 }

DataContextFactory

using System.Data.Linq;
using System.Web;public static class DataContextFactory
{
    
public static TDataContext GetDataContext<TDataContext>()
        
where TDataContext : DataContext, new()
    {
        
if (HttpContext.Current != null)
        {
            
string key = typeof(TDataContext).FullName;

            TDataContext dataContext 

= HttpContext.Current.Items[key] as TDataContext;
            
if (dataContext == null)
            {
                dataContext 
= new TDataContext();
                HttpContext.Current.Items[key] 
= dataContext;
            }
            
return dataContext;
        }
return new TDataContext();
    }
}