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(); } } } }