note:you can delete reference of entityframework when using this classes.it`s just a simple repohelper.the code below can also include a getpagedlist method when paging.
have fun,it`s simple,just create edmx file from database.all one sentence.but when using iqueryable or transaction,you need to use context factory and develop your own methods.
i am using it now.very simple.just like a static modelhelper orm.
1.simpledaterepo:
/*
* 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 }
}
 
 2.context factory:
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的更多相关文章

  1. Method not found: 'System.Data.Entity.ModelConfiguration.Configuration.XXX

    使用EF flument API  修改映射数据库字段的自增长 modelBuilder.Entity<Invoice>().Property(p => p.Id).HasDatab ...

  2. 报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败

    使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败.有关详细信息, ...

  3. 传入字典的模型项的类型为“System.Data.Entity.DynamicProxies.

    今天做东西遇到了,这样的一个问题,最后了半天才找到问题所在,现在给大家分享一下问题所在: 传入字典的模型项的类型为“System.Data.Entity.DynamicProxies.doctorUs ...

  4. 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 ...

  5. EF生成 类型“System.Data.Entity.DbContext”在未被引用的程序集中定义

    错误描述: 1 类型“System.Data.Entity.DbContext”在未被引用的程序集中定义.必须添加对程序集“EntityFramework, Version=5.0.0.0, Cult ...

  6. “System.Data.Entity.Internal.AppConfig"的类型初始值设定项引发异常。{转}

    <connectionStrings> <add name="ConnectionStringName" providerName="System.Da ...

  7. System.Data.Entity.Internal.AppConfig 类型初始值设定项引发异常

    在一开始时将connectionStrings 写在了configSections之上如下图一示,结果抛出:“System.Data.Entity.Internal.AppConfig”的类型初始值设 ...

  8. MVC中发生System.Data.Entity.Validation.DbEntityValidationException验证异常的解决方法

    发生System.Data.Entity.Validation.DbEntityValidationException这个异常的时候,如果没有用特定的异常类去捕捉,是看不到具体信息的. 通常都是用Sy ...

  9. System.Data.Entity.Infrastructure.DbUpdateException

    异常描述:   捕捉到 System.Data.Entity.Infrastructure.DbUpdateException  HResult=-2146233087  Message=无法更新 E ...

随机推荐

  1. Hive学习路线图

  2. [Bzoj4408]神秘数(主席树)

    Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数. 例如S={1,1,1,4,13}, 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = ...

  3. spring源码学习中的知识点

    一.循环依赖 循环依赖就是循环引用,就是两个或多个bean之间互相持有对方. 1.构造器循环依赖 表示通过构造器注入造成的循环依赖,此依赖是无法解决的,只能抛出BeanCurrentlyInCreat ...

  4. Wind Of Change

    Wind of change until the end  变革的风一直吹直至最后 You will see that I will be your friend 你会看见我成为你的朋友 If you ...

  5. SpringBoot推荐基础包

    技术交流群:233513714 Spring Boot 推荐的基础包 名称 说明 spring-boot-starter 核心 POM,包含自动配置支持.日志库和对 YAML 配置文件的支持. spr ...

  6. laravel5.5开发composer扩展包

    目录 1. 下载laravel框架,并命名(framework) 2. 创建相关目录 3. 项目根目录下的composer.json文件中声明命名空间 4. 在包的根目录(packages/arche ...

  7. 新工具填补Docker管理空白

    [TechTarget中国原创] 从事容器管理领域的IT运维专家这周需要评估一个新的工具. Docker推出了一款新产品,意在让IT运维人员能够跟上开发人员的脚步,这一产品是Docker Datace ...

  8. Python 3基础教程10-全局变量和局部变量

    本文来讲讲全局变量和局部变量,前面学习了函数的基本使用,所以,这里就要注意变量的使用和访问权限. 试试下面的demo.py

  9. Python 高级 I/O 多路复用

    Table of Contents 前言 select selectors 结语 参考链接 前言 第一次接触和 I/O 多路复用相关的概念是在书 CSAPP1 的并发编程章节,当时在了解了这个概念后只 ...

  10. Linux下vsftp匿名用户配置

    Linux下vsftp匿名用户上传和下载的配置 配置要注意三部分,请一一仔细对照: 1.vsftpd.conf文件的配置(vi /etc/vsftpd/vsftpd.conf) #允许匿名用户登录FT ...