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 ...
随机推荐
- php5.3升级脚本
在lanmp/wdcp/wdOS的当前版本中,默认的php都是用到5.2.17的版本如需要升级到php5.3的,可使用如下脚本升级(注:此升级无安全漏洞等原因,只为某些追求高版本或应用需求需要高版本, ...
- javascript的冒泡排序, 快速排序, 选择排序, 插入排序
冒泡排序, 最经典的排序, 把比较大的数字往后放, 和选择排序恰恰相反: <!DOCTYPE html> <html lang="en"> <head ...
- SHELL pv uv 统计事例
#!/bin/sh #statistics newplive logs SOURCELOGS=$ ];then echo echo "please input file!" ech ...
- JAVA遍历HashMap和ArrayList
List Map 基础信息 HashMap 最近写程序经常需要遍历集合,所以总结一下内容: 一.简单实现 Map map = new HashMap(); for(Object o : map.key ...
- Vue为什么没有templateUrl
Why Vue.js doesn't support templateURL Vue.js为什么不支持templateUrl模式 原因 templateUrl使用ajax的方式在运行时加载templa ...
- MapReduce三种路径输入
目前为止知道MapReduce有三种路径输入方式.1.第一种是通过一下方式输入: FileInputFormat.addInputPath(job, new Path(args[0]));FileIn ...
- Session Alerts
To create alerts for specific sessions, add rules using FiddlerScript. For example: Play a sound whe ...
- Java多线程之ReadWriteLock读写锁简介与使用教程
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6558073.html 普通的锁在对某一内容加锁后,其他线程是不能访问的.但是我们要考虑这种情况:如果当前加锁 ...
- Web Service 简介
最近使用ODI的工具箱中的ODIInvokeWebService.因此简单了解下WebService的理论知识. 一.Web Service 简介 Web Service就是可编程的URL,使用标准的 ...
- 关于java线程的daemon的认识
在 JAVA中的CountDownLatch.CyclicBarrier.Semaphore的简单测试 这文章里说到了线程的daemon问题,特写一篇来分析一下. 上代码: package com.y ...