public abstract class LYEntityDbContextwhere DB : DbContext, new() where T : LYEntityDbContext { public void GenerateHashCode() { var thisTime = this.GetType().GetProperties().Select(p => p.GetValue(this).GetHashCode()); this.changes.Add(thisTime); } private List > changes = new List >(); private bool executed = true; private bool hasChanged { get { if (this.changes.Count < 2) { return false; } var result = !this.changes.First().Except(this.changes.Last()).Any(); return result; } } public bool Executed() { return this.executed; } public static IEnumerable Find(Action action, params object[] keyValues) { return Find(action, true, keyValues); } public static IEnumerable Find(Action action, bool inBatch, params object[] keyValues) { if (inBatch) { using (var db = new DB()) { var hasChanged = false; for (int i = 0; i < keyValues.Length; i++) { var id = keyValues[i]; var t = db.Set ().Find(id); yield return t; t.GenerateHashCode(); action(t); t.GenerateHashCode(); if (!hasChanged) { hasChanged = t.hasChanged; } } if (hasChanged) { db.SaveChanges(); } } } else { foreach (var id in keyValues) { yield return Find(action, id); } } } public static T Find(Action action, object id) { using (var db = new DB()) { var t = db.Set ().Find(id); t.GenerateHashCode(); action(t); t.GenerateHashCode(); if (t.hasChanged) { try { db.SaveChanges(); t.executed = true; } catch (Exception) { t.executed = false; } } return t; } } public static IEnumerable FindAll (Func
where, Func orderby, int skip, int take, out int all) { using (var db = new DB()) { if (where == null) { var r = db.Set (); all = r.Count(); return r; } all = db.Set ().Count(where); if (orderby == null) { var r = db.Set ().Where(where); return r; } if (take <= 0) { var r = db.Set ().Where(where).OrderBy(orderby); return r; } if (skip <= 0) { var r = db.Set ().Where(where).OrderBy(orderby).Take(take); return r; } var rr = db.Set ().Where(where).OrderBy(orderby).Skip(skip).Take(take); return rr; } } }