public virtual void Delete(T data) { using (messageEntities db = new messageEntities()) { Type type = data.GetType(); PropertyInfo[] members = type.GetProperties(); List <string> ids = new List <string>(); foreach (PropertyInfo m in members) { var temp = m.GetCustomAttribute <KeyAttribute>(); if (temp != null) { object obj = m.GetValue(data); ids.Add(obj.ToString()); continue; } } T entity = db.Set <T>().Find(ids.ToArray()); if (entity != default(T)) { db.Set <T>().Remove(entity); db.SaveChanges(); } } }
public virtual void DeleteRange(IList <T> datas) { using (messageEntities db = new messageEntities()) { IList <T> list = new List <T>(); foreach (T data in datas) { Type type = data.GetType(); PropertyInfo[] members = type.GetProperties(); List <string> ids = new List <string>(); foreach (PropertyInfo m in members) { var temp = m.GetCustomAttribute <KeyAttribute>(); if (temp != null) { object obj = m.GetValue(data); ids.Add(obj.ToString()); continue; } } T entity = db.Set <T>().Find(ids.ToArray()); list.Add(entity); } if (list.Count > 0) { foreach (var l in list) { db.Set <T>().Remove(l); } db.SaveChanges(); } } }
public virtual IList <T> FindAll() { using (messageEntities entity = new messageEntities()) { return(entity.Set <T>().ToList()); } }
public virtual T Find(string id) { using (messageEntities db = new messageEntities()) { T entity = db.Set <T>().Find(id); return(entity); } }
public virtual T Add(T data) { using (messageEntities entity = new messageEntities()) { entity.Set <T>().Add(data); try { entity.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { throw ex; } return(data); } }
public virtual void AddRange(IList <T> datas) { using (messageEntities entity = new messageEntities()) { foreach (var data in datas) { entity.Set <T>().Add(data); } try { entity.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { throw ex; } } }