ef 通用类

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Infrastructure
{
    public class BaseRepository<T> where T : class
    {
        protected CCDbContext Context = CCDbContext.Current;


        public virtual IQueryable<T> GetAll()
        {
            var ls = Context.Set<T>();
            return ls;
        }
        public virtual IQueryable<T> GetAll(string includes)
        {
            if (!string.IsNullOrWhiteSpace(includes))
            {
                string[] ins = includes.Split(',');
                DbQuery<T> query = Context.Set<T>();
                foreach (string s in ins)
                {
                    query = query.Include(s);
                }
                return query;
            }
            return Context.Set<T>();
        }

        public void Add(T entity)
        {
            Context.Set<T>().Add(entity);
            Context.SaveChanges();
        }

        public virtual void Update(T entity)
        {
            var entry = Context.Entry(entity);
            Context.Set<T>().Attach(entity);
            entry.State = EntityState.Modified;
            Context.SaveChanges();
        }

        public virtual T Get(int id)
        {
            return Context.Set<T>().Find(new object[] { id });
        }

        public T Get(object[] ids)
        {
            return Context.Set<T>().Find(ids);
        }

        public virtual bool Del(int id)
        {
            var entity = Context.Set<T>().Find(new object[] { id });

            if (entity == null)
                return false;

            Context.Set<T>().Remove(entity);
            Context.SaveChanges();
            return true;
        }

        public virtual bool Del(int[] ids)
        {
            if (ids == null || ids.Length <= 0)
            {
                return false;
            }
            var changed = false;
            foreach (var id in ids)
            {
                var entity = Get(id);
                if (entity != null)
                {
                    Context.Set<T>().Remove(entity);
                    changed = true;
                }
            }
            if (changed)
                Context.SaveChanges();

            return changed;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Domain.Course.Entry;
using Domain.User.Entry;

namespace Domain.Infrastructure
{
    public class CCDbContext : DbContext
    {
        public CCDbContext()
            : base("name=conn")
        {
        }

        private static ThreadLocal<CCDbContext> _threadLocal;
        private static CCDbContext _current;
        public static CCDbContext Current
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    // 非 web 环境
                    if (_threadLocal == null)
                    {
                        _threadLocal = new ThreadLocal<CCDbContext>(() => new CCDbContext());
                    }
                    _current = _threadLocal.Value;
                    return _threadLocal.Value;
                }
                else
                {
                    var cached = HttpContext.Current.Items["_CCDbContext"];
                    if (cached != null && cached is CCDbContext)
                    {
                        _current = cached as CCDbContext;
                        return _current;
                    }
                    else
                    {
                        var context = new CCDbContext();
                        HttpContext.Current.Items["_CCDbContext"] = context;
                        _current = context;
                        return context;
                    }
                }
            }
        }

        public static void DisposeCurrent()
        {
            if (_current != null)
                _current.Dispose();
        }

        #region 所有实例
        public DbSet<Word> Words { get; set; }

        public DbSet<WordTranslation> WordTranslations { get; set; }

        public DbSet<Language> Languages { get; set; }

        public DbSet<Catalog> Catalogs { get; set; }

        public DbSet<CatalogName> CatalogNames { get; set; }

        public DbSet<Term> Terms { get; set; }

        public DbSet<TermTranslation> TermTranslations { get; set; }

        public DbSet<Lesson> Lessons { get; set; }

        public DbSet<LessonWord> LessonWords { get; set; }

        public DbSet<RelevantTerm> RelevantTerms { get; set; }

        public DbSet<Domain.User.Entry.User> Users { get; set; }

        public DbSet<Group> Groups { get; set; }

        public DbSet<GroupTranslation> GroupTranslation { get; set; }

        public DbSet<TermGroup> TermGroup { get; set; }

        public DbSet<RelevantGroup> RelevantGroups { get; set; }

        public DbSet<Derivations> Derivations { get; set; }

        public DbSet<RelatedWord> RelatedWord { get; set; }
        public DbSet<Role> Role { get; set; }

        public DbSet<wordsh> wordsh { get; set; }
        public DbSet<shinfo> shinfo { get; set; }

        public DbSet<Part> Part { get; set; }
        public DbSet<Radical> Radical { get; set; }
        public DbSet<WordPart> WordPart { get; set; }
        #endregion
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值