zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    字段修改名称
    coercing to Unicode: need string or buffer, geoprocessing value object found
    为什么ArcGIS 10.3导出 Shapefile的字段名会被截断成3个汉字?解决方法如下
    arcgis python 使用光标和内存中的要素类将数据加载到要素集 学习:http://zhihu.esrichina.com.cn/article/634
    arcgis python 获得arcgis安装版本和安装位置
    arcgis python 不知道一个工具怎么用
    arcgis 地理坐标系 699个,投影坐标系是4767
    arcgis python 参数类型和含义
    win10 svn commit无响应
    新建网站与新建Asp.Net Web 应用程序的区别
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/4730712.html
Copyright © 2011-2022 走看看