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
    }
}
时间: 2024-10-12 17:13:43

ef 通用类的相关文章

EF(Entity Framework)通用DBHelper通用类,增删改查以及列表

(1)通用类用法 其中 通用类名:DBhelper 实体类:UserInfo 1 //新增 2 DBHelper<UserInfo> dbhelper = new DBHelper<UserInfo>(); 3 UserInfo userinfo = new UserInfo(); 4 userinfo.Name = "1"; 5 dbhelper.Add(userinfo); 6 7 //根据条件查找列表 8 var entityes = dbhelper.F

EF 通用数据层类

EF 通用数据层父类方法小结 转载:http://www.cnblogs.com/yq-Hua/p/4165344.html MSSql 数据库 数据层 父类 增删改查: using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using Syste

EF通用数据层封装类(支持读写分离,一主多从)

浅谈orm 记得四年前在学校第一次接触到 Ling to Sql,那时候瞬间发现不用手写sql语句是多么的方便,后面慢慢的接触了许多orm框架,像 EF,Dapper,Hibernate,ServiceStack.OrmLite 等.当然每种orm都有各自的优势,也有不足的地方.园子里也有很多大神开源了他们写的orm,如SqlSugar,Chloe.ORM,CYQ.Data 等.先不说这些开源的orm使用度怎么样,我觉得起码从开源的精神上就很可嘉了,我也曾下载过这几位大神的源码进行学习. 所有o

ASP.net如何保证EF操作类线程内唯一

说到线程内唯一,肯定会想到单例模式,但是如果多用户访问网站就会出现问题.ASP.net中有两种方法可以保证EF操作类线程内唯一(目前只会这两种,以后有好的方法再添加): 1.httpcontext(实现原理也是通过数据槽callcontext) 将EF操作类通过键值对方法保存在HttpContext.Current.Items["key"],封装成方法直接调用 2.callcontext public static DbContext CreateDbContext() { DbCon

C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration; namespace XXX{    /// <summary>    /// 针对SQL Server数据库操作的通用类           /// </sum

jQuery Ajax封装通用类 (linjq)

jQuery Ajax封装通用类 (linjq) $(function(){ /** * ajax封装 * url 发送请求的地址 * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1} * async 默认值: true.默认设置下,所有请求均为异步请求.如果需要发送同步请求,请将此选项设置为 false. * 注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行. * t

poi导出excel通用类

一.关键的通用类public class PoiExportUtils {    private static HSSFWorkbook workBook; public PoiExportUtils(){        workBook = new HSSFWorkbook();    }        /**     * 创建sheet     * @param sheetName     * @param workBook     * @return     */    public st

PHP curl get post通用类

<?php /** * @author:xiaojiang * curl 通用方法 ..get /post 传送数据 */ class process{ const GET = 0; const POST = 1; public $url; public $ch = null; private $type = 1; public function __construct( $url , $type = self::POST){ $this->url = $url; $this->ch =

DataTable转换为List&lt;Model&gt;的通用类

在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有模型类型检索方便. 所以很多人都是按照以下方式做的: // 获得查询结果DataTable dt = DbHelper.ExecuteDataTable(...);// 把DataTable转换为IList<UserInfo>IList<UserInfo> users = ConvertToUserInfo(dt); 问题:如果此系统有几十上百个模型,那不是每个模型中都要写个把DataTable转换为