/// <summary> /// Find entity using numerci id /// </summary> /// <typeparam name="T">Entity to find</typeparam> /// <param name="id"></param> /// <returns>The entity</returns> protected T FindById <T> (LeatherContext ctx, long id) { var prop = ctx.GetType().GetProperty(typeof(T).Name); var getter = prop.GetGetMethod(); var dbset = getter.Invoke(ctx, null); var findMethod = dbset.GetType().GetMethod("Find"); var result = findMethod.Invoke(dbset, new object[] { new object[] { id } }); return((T)result); }
protected TEntity Save(LeatherContext ctx, TEntity entity) { if (entity.GetType().GetProperty("Id") != null) { int?id = (int)entity.GetType().GetProperty("Id").GetMethod.Invoke(entity, null); if (id != null && id > 0) { var updated = ctx.Set <TEntity>().Attach(entity); ctx.Entry(updated).State = EntityState.Modified; return(updated); } } var saved = ctx.Set <TEntity>().Add(entity); return(saved); }
protected List <TEntity> SelectAll(LeatherContext ctx, int max) { return(ctx.Set <TEntity>().Take(max).ToList()); }
protected List <TEntity> SelectAll(LeatherContext ctx) { return(SelectAll(ctx, 500)); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> protected TEntity SelectById(LeatherContext ctx, int id) { return(ctx.Set <TEntity>().Find(id)); }