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 ...
随机推荐
- .NET 托管、非托管、本地:这些代码有什么区别?
http://www.codeguru.com/Csharp/.NET/cpp_managed/article.php/c4871 本文内容 什么是托管代码? 什么是非托管代码? 什么是本地代码? 托 ...
- Unity3D 学习 创建简单的按钮、相应事件
选择file -->new project 然后保存到相应的地方 下面是这个刚创建的工程效果图. 然后创建一个C# Script ||定位到最左下角找到 assets --> creat ...
- jQuery.cookie应用操作
//1.插件框架: /* * name @键 * value @值 * options @选项,包括有效期 路径 域名等 */ jQuery.cookie = function(name, value ...
- 让你的ASP.NET虚拟主机也支持子网站
现在asp.net虚拟主机一般都可以绑定多个域名,但是通过这几个域名打开的页面都一样.如何使绑的这几个域名分别打开不通的页面(即实现子网站的功能)呢? 其实很简单,只需4个步骤: 1)给虚拟主机 ...
- Android Studio 之 打包生成的 apk 安装包装到手机上闪退
今天,在 Android Studio 中的模拟器中测试 app 程序正常,然后打包 apk 安装包程序,发给领导后,领导反馈安装后打开闪退,抓紧安装到自己手机上,发现果然存在闪退.查阅资料后,解决方 ...
- Ganglia监控Hadoop集群的安装部署
一. 安装环境 Ubuntu server 12.04 安装gmetad的机器:192.168.52.105 安装gmond的机器:192.168.52.31,192.168.52.32,192.16 ...
- 009-Go 读取写入CSV文件
package main import( "encoding/csv" "fmt" "os" "strconv" ) t ...
- dir for RequestHandler and request
两个对象的dir # RequestHandler ['GET', 'GETPOST', 'POST', 'SUPPORTED_METHODS', '_ARG_DEFAULT', '_INVALID_ ...
- MySQL快速建立测试表
1:只要已经存在表结构的 第一种方式: CREATE TABLE T1 SELECT * FROM mysql.user ; 第二种方式: CREATE TABLE T2 LIKE mysql.use ...
- java 数组声明方法
//数组 public class Test16{ public static void main(String args[]){ //声明一: int [] x; x = new int[3];// ...