zoukankan      html  css  js  c++  java
  • 用EF 搭建底层

    用EF 搭建底层

    接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Linq.Expressions;
    
    namespace Project.DAL
    {
        interface IBaseService<T> where T:class,new()
        {
            T Find(params object[] keys);
            IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where);
            IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where);
            IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where);
            void Insert(T t);
            void Insert(IEnumerable<T> t);
            void Update(T t);
            void Update(IEnumerable<T> t);
            void Delete(T t);
            void Delete(IEnumerable<T> t);
            bool SaveChange();
        }
    }
    

    底层

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Linq.Expressions;
    using System.Data.Entity;
    using System.Runtime.Remoting.Messaging;
    
    namespace Project.DAL
    {
        public class BaseService<T> : IBaseService<T> where T : class, new()
        {
    
            Models.MyAll1817Entities dbContext = DbContextFactory.Create() as Models.MyAll1817Entities;
    
            public T Find(params object[] keys)
            {
                return dbContext.Set<T>().Find(keys);
            }
            public IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where) 
            {
                IQueryable<T> iq = dbContext.Set<T>();
                if (where != null || where.Length < 1)
                {
                    foreach (var item in where)
                    {
                        iq = iq.Where(item);
                    }
                }
                return iq;
            }
    
            public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where)
            {
                var iq = QueryAll(where);
                if (isAsc)
                {
                    iq = iq.OrderBy(order);
                }
                else
                {
                    iq = iq.OrderByDescending(order);
                }
                return iq;
            }
    
            public IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where)
            {
                var iq = QueryAll<type>(order, isAsc, where);
                total = iq.Count();
                return iq.Skip(skip).Take(take);
            }
    
            public void Insert(T t)
            {
                dbContext.Set<T>().Add(t);
            }
    
            public void Insert(IEnumerable<T> t)
            {
                dbContext.Set<T>().AddRange(t);
            }
            public void Update(T t)
            {
                dbContext.Entry<T>(t).State = EntityState.Modified;
            }
    
            public void Update(IEnumerable<T> t)
            {
                foreach (var item in t)
                {
                    dbContext.Entry<T>(item).State = EntityState.Modified;
                }
            }
    
            public void Delete(T t)
            {
                dbContext.Set<T>().Remove(t);
            }
    
            public void Delete(IEnumerable<T> t)
            {
                dbContext.Set<T>().RemoveRange(t);
            }
    
            /// <summary>
            /// 保存修改
            /// </summary>
            /// <returns></returns>
            public bool SaveChange()
            {
                //以下try注释删掉的话Debug时不会报错
                //请正式上线时删掉注释
                //try
                //{
                    dbContext.SaveChanges();
                    return true;
                //}
                //catch (Exception e)
                //{
                //    return false;
                //}
            }
        }
    }
    

    保证EF上下文对象在线程内是唯一的

    这里比linq要多一个类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity;
    using System.Runtime.Remoting.Messaging;
    using Project.Models;
    
    
    namespace Project.DAL
    {
        public class DbContextFactory
        {
            /// <summary>
            /// 创建EF上下文对象,已存在就直接取,不存在就创建,保证线程内是唯一。
            /// </summary>
            public static DbContext Create()
            {
                DbContext dbContext = CallContext.GetData("DbContext") as DbContext;
                if (dbContext == null)
                {
                    dbContext = new MyAll1817Entities ();
                    CallContext.SetData("DbContext", dbContext);
                }
                return dbContext;
            }
        }
    }
    
    
  • 相关阅读:
    Ceph对象主本损坏的修复方法
    mds的cpu占用问题分析以及解决办法
    CentOS GRUB损坏修复方法
    掉电后osdmap丢失无法启动osd的解决方案
    怎样禁止Ceph OSD的自动挂载
    Silverlight 预定义颜色速查表
    Silverlight中Image控件Stretch属性的四种值比较
    Silverlight中图片显示
    Silverlight中关于ComboBox的各种使用
    Silverlight样式定义
  • 原文地址:https://www.cnblogs.com/setsuna-cn/p/12256196.html
Copyright © 2011-2022 走看看