MVC 創建線程內的db單例
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using LSUnion.Site.Models; 6 7 namespace LSUnion.Site.WebHelper 8 { 9 using System.Runtime.Remoting.Messaging; 10 public class Factory 11 { 12 public static LSUnionEntities GetDbContext() 13 { 14 object db = CallContext.GetData("LSUnionEntities"); 15 if (db==null) 16 { 17 db = new LSUnionEntities(); 18 CallContext.SetData("LSUnionEntities",db); 19 } 20 return db as LSUnionEntities; 21 } 22 } 23 }