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 <= )
{
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
}
}
ef 通用类的更多相关文章
- EF(Entity Framework)通用DBHelper通用类,增删改查以及列表
其中 通用类名:DBhelper 实体类:UserInfo 1 //新增 2 DBHelper<UserInfo> dbhelper = new DBHelper<UserInfo& ...
- EF 通用数据层类
EF 通用数据层父类方法小结 转载:http://www.cnblogs.com/yq-Hua/p/4165344.html MSSql 数据库 数据层 父类 增删改查: using System; ...
- EF通用数据层封装类(支持读写分离,一主多从)
浅谈orm 记得四年前在学校第一次接触到 Ling to Sql,那时候瞬间发现不用手写sql语句是多么的方便,后面慢慢的接触了许多orm框架,像 EF,Dapper,Hibernate,Servic ...
- 关于EF 通用增删改查的封装
1. Entity Framework是Microsoft的ORM框架,随着 Entity Framework 不断的完善强化已经到达了EF 6.0+ 还是非常的完善的,目前使用的比例相对于其他OR ...
- ASP.net如何保证EF操作类线程内唯一
说到线程内唯一,肯定会想到单例模式,但是如果多用户访问网站就会出现问题.ASP.net中有两种方法可以保证EF操作类线程内唯一(目前只会这两种,以后有好的方法再添加): 1.httpcontext(实 ...
- poi导出excel通用类
一.关键的通用类public class PoiExportUtils { private static HSSFWorkbook workBook; public PoiExportUtils ...
- NPOI MVC 模型导出Excel通用类
通用类: public enum DataTypeEnum { Int = , Float = , Double = , String = , DateTime = , Date = } public ...
- MVC NPOI Linq导出Excel通用类
之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...
- NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...
随机推荐
- oracle expdp/impdp 用法详解
http://hi.baidu.com/hzfsai/item/4a4b3fc4b1cf7e51ad00efbd oracle expdp/impdp 用法详解 Data Pump 反映了整个导出/导 ...
- 微信小程序 - 日期(起止)选择器组件
2019-01-03 : 修复了日期day-1,新增了年月日(除去时分秒),删除了不必要的touchmove 新增: column: ""(年月日) 配置: pickerConfi ...
- 微信小程序 - async/await
下面只是做一些介绍以及使用的原因,详情介绍还请移步博主:https://www.cnblogs.com/SamWeb/p/8417940.html regenerator-runtime下载:http ...
- vmware产品框架-计算中心,5.1更新等
概述:SRM,5.1新特性,vCenter Operations的介绍等 5.1改进参见:http://wenku.baidu.com/view/26530362a98271fe910ef961.ht ...
- web前端开发,如何提高页面性能优化?
内容方面: 1.减少 HTTP 请求 (Make Fewer HTTP Requests) 2.减少 DOM 元素数量 (Reduce the Number of DOM Elements) 3.使得 ...
- 〖Linux〗使用纯命令行来操作VBOX(宿主机不需要X11 Server)
1. Linux安装vbox,略过 2. 查看已安装扩展插件 VBoxManage list extpacks 3. 创建一个vm: VBoxManage createvm --name " ...
- ROS中发布激光扫描消息
激光雷达工作时会先在当前位置发出激光并接收反射光束,解析得到距离信息,而后激光发射器会转过一个角度分辨率对应的角度再次重复这个过程.限于物理及机械方面的限制,激光雷达通常会有一部分“盲区”.使用激光雷 ...
- 6、redis之使用spring-data-redis的Template
POM: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...
- Qt Installer Framework 使用说明(三)
目录 6.Qt Installer Framework 示例 7.参考 Reference 配置文件 Configuration File 配置文件元素的简要说明 Summary of Configu ...
- ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
[root@ldaptest openldap]# ldapadd -x -D "cn=admin,dc=ultrapower,dc=com" -W -f /tmp/base.ld ...