写一个system.data.entity的simpledatarepo,实现crudq这些功能,不需要引入entityframework,直接可以使用,用到objectset
/*
* author:iGo
* for-free
* last-update:2015年7月7日
* auth:mit
* hope it will be useful to you
* */
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions; namespace DBContext {
/// <summary>
/// static data repo,you donot need to reference entity framework
/// </summary>
public class SimpleDataRepo {
public static void Add<T>(T model) {
if (model == null) return;
using (var context = DataContext.Context) {
context.AddObject(typeof(T).Name, model);
context.SaveChanges();
}
} public static void Edit<T>(T model) where T : class {
if (model == null) return;
using (var context = DataContext.Context) {
context.AddObject(model.GetType().Name, model);
context.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
context.SaveChanges();
}
}
public static void EditCommand<T>(String setCondition, String whereClause) {
if (String.IsNullOrEmpty(setCondition)) return;
using (var context = DataContext.Context) {
context.ExecuteStoreCommand(String.Format("update {0} set {1} where {2}", typeof(T).Name, setCondition, whereClause));
}
} public static void Delete<T>(T model) where T : class {
if (model == null) return;
using (var context = DataContext.Context) {
context.AddObject(model.GetType().Name, model);
context.ObjectStateManager.ChangeObjectState(model, EntityState.Deleted);
context.SaveChanges();
} } [NotUsed("not used due to linq function unsupported!")]
private static int GetPropertyIdValueFromModel<T>(T model) {
return (int)model.GetType().GetProperty("id").GetValue(model, null);
} public static IList<T> GetAll<T>(Expression<Func<T, bool>> condition = null) {
using (var context = DataContext.Context) {
var query = context.GetType().GetProperty(typeof(T).Name).GetValue(context, null) as IQueryable<T>;
return query != null ? query.ToList() : null;
}
} public static IList<T> GetAllCommand<T>(String whereClause = "") {
using (var context = DataContext.Context) {
return context.ExecuteStoreQuery<T>(String.Format("select * from {0} {1}", typeof(T).Name,
String.IsNullOrEmpty(whereClause) ? "" : (" where " + whereClause))).ToList();
}
} /// <summary>
/// Get first model that satisfies the specified condition,GetById can be implied here,ie:a=>a.Id=5
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="condition"></param>
/// <returns></returns>
public static T QueryFirst<T>(Expression<Func<T, bool>> condition) {
using (var context = DataContext.Context) {
var query = context.GetType().GetProperty(typeof(T).Name).GetValue(context, null) as IQueryable<T>;
if (query == null) return default(T);
return query.FirstOrDefault(condition);
}
} public static T QueryFirstCommand<T>(String condition = "") {
using (var context = DataContext.Context) {
return
context.ExecuteStoreQuery<T>(String.Format("select top 1 * from {0} {1}", typeof(T).Name,
String.IsNullOrEmpty(condition) ? "" : (" where " + condition))).FirstOrDefault();
}
} public static IList<T> QuerySort<T, TS>(Expression<Func<T, bool>> condition, Expression<Func<T, TS>> sortExpression = null, bool isDecending = false) {
using (var context = DataContext.Context) {
var query = context.GetType().GetProperty(typeof(T).Name).GetValue(context, null) as IQueryable<T>;
if (query == null) return null;
query = query.Where(condition).Where(condition);
if (sortExpression != null)
query = isDecending ? query.OrderByDescending(sortExpression) : query.OrderBy(sortExpression);
return query.ToList();
}
} public static void DeleteByIds<T>(String ids) {
if (String.IsNullOrEmpty(ids)) throw new ArgumentException("ids cannot be null or empty!");
using (var context = DataContext.Context) {
context.ExecuteStoreCommand(String.Format("delete from {0} where id in ({1})", typeof(T).Name, ids));
context.SaveChanges();
}
} #region web paging field //protected virtual IPagedList<T> GetTPagedList<T, TS>(Expression<Func<T, bool>> filter = null, Expression<Func<T, TS>> orderby = null, bool isDecending = false) where T : new() {
// IPagedList<T> list;
// using (var context = DataContext.Context) {
// var pageIndex = Request["pageNum"] ?? "1";
// var pageSize = Request["numPerPage"] ?? "15"; // Expression<Func<T, bool>> condition = a => true;
// if (filter != null) condition = condition.And(filter);
// var b = context.GetType().GetProperty(typeof(T).Name).GetValue(context, null) as IQueryable<T>;
// if (b == null) return null;
// b = b.Where(condition);
// if (orderby != null)
// b = !isDecending ? b.OrderBy(orderby) : b.OrderByDescending(orderby);
// list = b.ToPagedList(int.Parse(pageIndex), int.Parse(pageSize));
// //b.Skip(int.Parse(pageSize)*(int.Parse(pageIndex) - 1)).Take(int.Parse(pageSize)).ToList();
// }
// return list;
//}
#endregion }
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text; namespace DBContext {
public partial class DataContext {
public static GpsDataContext Context {
get {
return new GpsDataContext();
}
}
}
}
写一个system.data.entity的simpledatarepo,实现crudq这些功能,不需要引入entityframework,直接可以使用,用到objectset的更多相关文章
- Method not found: 'System.Data.Entity.ModelConfiguration.Configuration.XXX
使用EF flument API 修改映射数据库字段的自增长 modelBuilder.Entity<Invoice>().Property(p => p.Id).HasDatab ...
- 报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败
使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败.有关详细信息, ...
- 传入字典的模型项的类型为“System.Data.Entity.DynamicProxies.
今天做东西遇到了,这样的一个问题,最后了半天才找到问题所在,现在给大家分享一下问题所在: 传入字典的模型项的类型为“System.Data.Entity.DynamicProxies.doctorUs ...
- System.Security.SecurityException The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception
[15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception ...
- EF生成 类型“System.Data.Entity.DbContext”在未被引用的程序集中定义
错误描述: 1 类型“System.Data.Entity.DbContext”在未被引用的程序集中定义.必须添加对程序集“EntityFramework, Version=5.0.0.0, Cult ...
- “System.Data.Entity.Internal.AppConfig"的类型初始值设定项引发异常。{转}
<connectionStrings> <add name="ConnectionStringName" providerName="System.Da ...
- System.Data.Entity.Internal.AppConfig 类型初始值设定项引发异常
在一开始时将connectionStrings 写在了configSections之上如下图一示,结果抛出:“System.Data.Entity.Internal.AppConfig”的类型初始值设 ...
- MVC中发生System.Data.Entity.Validation.DbEntityValidationException验证异常的解决方法
发生System.Data.Entity.Validation.DbEntityValidationException这个异常的时候,如果没有用特定的异常类去捕捉,是看不到具体信息的. 通常都是用Sy ...
- System.Data.Entity.Infrastructure.DbUpdateException
异常描述: 捕捉到 System.Data.Entity.Infrastructure.DbUpdateException HResult=-2146233087 Message=无法更新 E ...
随机推荐
- 3.4.2 Undefined类型【JavaScript高级程序设计第三版】
Undefined 类型只有一个值,即特殊的 undefined.在使用 var 声明变量但未对其加以初始化时,这个变量的值就是 undefined,例如: var message; alert(me ...
- yii rbac
一.简介 什么是rbac ? rbac是就是基于角色的访问控制. yii提供一套基础的底层接口,我们知道,rbac经历好几个阶段,从rbac0到rbac3,从基础的用户.角色.权限,到动态的rbac处 ...
- PHP的IMAP函数
imap_8bit -转换的8位字符串的引用,打印字符串 imap_alerts -返回所有的I MAP邮件警报已经发生 imap_append -附加了一系列的信息到指定邮箱 imap_base64 ...
- 图解HTTP总结(3)——HTTP报文内的HTTP信息
HTTP通信过程包括从客户端发往服务端的请求及从服务器端返回客户端的响应. 用于HTTP协议交互的信息被称为HTTP报文.客户端的HTTP报文叫做请求报文,服务器端的叫做响应报文.HTTP报文本身是多 ...
- 学习Pytbon第十八篇,异常处理
什么是异常? 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行. 一般情况下,在Python无法正常处理程序时就会发生一个异常. 异常是Python对象,表示一个错误. 当Pyth ...
- 交叉编译qt5.6
按照网上的攻略编译QT5.6 https://www.lijingquan.net/2016/07/08/build-kernel-busybox-qt5-6-tslib-imx28/ 出现问题,找不 ...
- APUE中对出错函数的封装
// 输出至标准出错文件的出错处理函数static void err_doit(int, int, const char *, va_list); /* * Nonfatal error relate ...
- 笔记-python-built-in functions-eval,exec,compile
笔记-python-built-in functions-eval,exec,compile 1. python代码执行函数 有时需要动态改变代码,也就是说代码需要是字符串格式,然后在按需要 ...
- 大话目标检测经典模型(RCNN、Fast RCNN、Faster RCNN)
目标检测是深度学习的一个重要应用,就是在图片中要将里面的物体识别出来,并标出物体的位置,一般需要经过两个步骤:1.分类,识别物体是什么 2.定位,找出物体在哪里 除了对单个物体进行检测,还要能支持 ...
- Javascript Step by Step - 01
基本数据类型 简单数值类型: undefined, null, boolean, number和string,共有5种 复合数据类型:object,array,function typeof操作符用来 ...