/// <summary> /// Get the current query value bindings. /// </summary> /// <returns>A collection of columns and values.</returns> private Dictionary <string, dynamic> PrepareBindings() { PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); Dictionary <string, dynamic> bindings = properties.Where(p => p.PropertyType.IsValueType || p.PropertyType == typeof(string)).ToDictionary(p => Attribute.IsDefined(p, typeof(ColumnAttribute)) ? p.GetCustomAttribute <ColumnAttribute>().Name : p.Name.ToSnakeCase(), p => p.GetValue(this)); bindings.Remove(PrimaryKeyName.ToSnakeCase()); return(bindings); }
/// <summary> /// Asynchronously find a model by its primary key. /// </summary> /// <param name="id">The value of the primary key.</param> /// <returns>An instance of the hydrated model.</returns> public static async Task <T> FindAsync(int id) { return(await Query.Where(PrimaryKeyName.ToSnakeCase(), id).FirstAsync()); }
/// <summary> /// Find a model by its primary key. /// </summary> /// <param name="id">The value of the primary key.</param> /// <returns>An instance of the hydrated model.</returns> public static T Find(int id) { return(Query.Where(PrimaryKeyName.ToSnakeCase(), id).First()); }
/// <summary> /// Perform a model update operation asynchronously. /// </summary> private async Task PerformUpdateAsync() { await Query.Where(PrimaryKeyName.ToSnakeCase(), PrimaryKey).UpdateAsync(PrepareBindings()); }
/// <summary> /// Perform a model update operation. /// </summary> private void PerformUpdate() { Query.Where(PrimaryKeyName.ToSnakeCase(), PrimaryKey).Update(PrepareBindings()); }