public virtual T Insert <T>(T parm) where T : DBBaseEntity, new() { try { DBEntityPropertys.GetKeys(typeof(T)).ForEach(o => { if (o.GetValue(parm) == null) { throw new DBException("主键不能为空"); } }); DBContext.Set <T>().Add(parm); DBContext.SaveChanges(); return(parm); } catch (DbEntityValidationException ex) { throw ex; } }
public virtual T Update <T>(T parm) where T : DBBaseEntity, new() { //try //{ Dictionary <string, object> KeyValues = new Dictionary <string, object>(); DBEntityPropertys.GetKeys(typeof(T)).ForEach(o => { KeyValues.Add(o.Name, o.GetValue(parm)); if (KeyValues[o.Name] == null) { throw new DBException("主键不能为空"); } }); DbEntityEntry <T> entry = DBContext.Entry <T>(parm); if (entry.State == EntityState.Detached) { DBContext.Set <T>().Attach(parm); } typeof(T).GetProperties().Where(o => !KeyValues.ContainsKey(o.Name)).ToList().ForEach(o => { if (o.GetValue(parm) != null) { entry.Property(o.Name).IsModified = true; } }); DBContext.SaveChanges(); return(parm); //} // catch(DbEntityValidationException ex) // { // throw ex; // } }
public virtual int Delete <T>(T parm) where T : DBBaseEntity, new() { Dictionary <string, object> KeyValues = new Dictionary <string, object>(); DBEntityPropertys.GetKeys(typeof(T)).ForEach(o => { KeyValues.Add(o.Name, o.GetValue(parm)); if (KeyValues[o.Name] == null) { throw new DBException("主键不能为空"); } }); var where = string.Empty; if (KeyValues.Count == 0) { throw new DBException("没有主键的表无法使用该方法"); } KeyValues.ToList().ForEach(o => { where += o.Key + "='" + o.Value.ToString() + "' and "; }); where = where.Remove(where.Length - 4); var attr = typeof(T).GetCustomAttribute(typeof(TableAttribute)); if (attr == null) { throw new DBException(typeof(T).Name + "类未设置TableAttribute"); } var sql = string.Format("delete from {0} where {1}", (attr as TableAttribute).Name, where); var seccuess = DBContext.Database.ExecuteSqlCommand(sql); if (seccuess < 0) { throw new DBException("数据未能删除"); } return(seccuess); }
public T GetByKey <T>(T parm) where T : DBBaseEntity, new() { Dictionary <string, object> KeyValues = new Dictionary <string, object>(); DBEntityPropertys.GetKeys(typeof(T)).ForEach(o => { KeyValues.Add(o.Name, o.GetValue(parm)); if (KeyValues[o.Name] == null) { throw new DBException("主键不能为空"); } }); var where = string.Empty; if (KeyValues.Count == 0) { throw new DBException("没有主键的表无法使用该方法"); } KeyValues.ToList().ForEach(o => { where += o.Key + "='" + o.Value.ToString() + "' and "; }); where = where.Remove(where.Length - 4); return(DBContext.Database.SqlQuery <T>("select * from {0} where {1}", typeof(T).GetType().Name, where).FirstOrDefault()); }