/// <summary> /// 根据id删除实体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="id"></param> public static void DeleteEntry <TEntity, T>(T id) where TEntity : class { using (BaseContext _db = new BaseContext()) { TEntity entity = _db.Set <TEntity>().Find(id); _db.Set <TEntity>().Attach(entity); _db.Set <TEntity>().Remove(entity); _db.SaveChanges(); } }
public static int UpdateEntryByProperty <T>(this BaseContext _db, T entity, string EntityKey) where T : class { DbSet <T> dbSet = _db.Set <T>(); dbSet.Attach(entity); MemberInfo[] members = entity.GetType().GetMembers(); IEnumerable <MemberInfo> properties = members.Where(m => m.MemberType == MemberTypes.Property && m.Name != EntityKey); foreach (MemberInfo mInfo in properties) { object o = entity.GetType().InvokeMember(mInfo.Name, BindingFlags.GetProperty, null, entity, null); if (o != null) { if (o.GetType().IsPrimitive || o.GetType().IsPublic) { try { DbEntityEntry entry = _db.Entry <T>(entity); entry.Property(mInfo.Name).IsModified = true; } catch { } } } } return(_db.SaveChanges()); }
/// <summary> /// 获取单个实体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public static TEntity getEntryById <TEntity, T>(T id) where TEntity : class { using (BaseContext _db = new BaseContext()) { return(_db.Set <TEntity>().Find(id)); } }
public static bool AddModel <TEntity>(List <TEntity> entry) where TEntity : class { using (BaseContext _db = new BaseContext()) { _db.Set <TEntity>().AddRange(entry); _db.SaveChanges(); return(true); } }
public static TEntity AddModel <TEntity>(TEntity entry) where TEntity : class { using (BaseContext _db = new BaseContext()) { _db.Set <TEntity>().Add(entry); _db.SaveChanges(); return(entry); } }