14---------单例添加一个EFContext上下文对象

/// <summary>
        /// 获取上下文对象的类
        /// </summary>
        public class ContextFactory
        {
            private static TestOAEntities context;
            public static DbContext GetContext()
            {
                if (context == null)
                {
                    context = new TestOAEntities();
                }
                return context;

            }
        }
        /// <summary>
        /// 第二种 借助static只执行一次的功能
        /// </summary>
        public class ContextFactory2
        {
            public static DbContext Context { get; set; }
            static ContextFactory2()
            {
                Context = new TestOAEntities();
            }
        }

 

posted on 2016-04-20 15:43  努力的活着_在人间  阅读(804)  评论(0)    收藏  举报

导航