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.Data.Entity; using System.Data.Objects; using System.Data.Objects.DataClasses; using System.Linq; using System.Linq.Expressions; using System.Net.Configuration; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Remoting.Contexts; using System.Text; using DBContext; namespace DBContext { /// <summary> /// static data repo,you donot need to reference entity framework /// </summary> public class SimpleDataRepo { public static void Add<T>(T model) where T : IEntityWithKey { 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 Delete<T>(T model) where T : IEntityWithKey { if (model == null) return; using (var context = DataContext.Context) { context.Attach(model); context.DeleteObject(model); 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; } } /// <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 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(); } } } }
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(); } } } }
时间: 2024-09-29 19:43:22