/// <summary> /// 从实体池中获取实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="key">标识符</param> /// <returns></returns> public static T GetDbContext <T>(object key) where T : DbContext, new() { string fullname = typeof(T).FullName; string connName = ""; string lastName = fullname.Split('.').Last(); foreach (ConnectionStringSettings item in System.Configuration.ConfigurationManager.ConnectionStrings) { if (lastName.StartsWith(item.Name)) { connName = item.Name; break; } } if (string.IsNullOrEmpty(connName)) { throw new Exception(string.Format("配置文件中不包含{0}的链接字符串", fullname)); } lock (l) { if (_DbContextPool.ContainsKey(connName)) { var pool = (DbContextPool <T>)_DbContextPool[connName]; return(pool.Get(key)); } else { var pool = new DbContextPool <T>(connName); _DbContextPool.Add(connName, pool); return(pool.Get(key)); } } }