public static Task <T2> UpdateAsync <T, T2>(this DbContext context, PropertyWatcher <T> Saved, Expression <Func <T, T2> > KeyField, T2 KeyValue, Expression <Func <T, T2> > OutputColumn, bool OutputInserted) { string table = GetTableName(typeof(T), context); string query = string.Format("UPDATE {0} SET {1} OUTPUT {3}.{4} WHERE {2} = @p0", table, string.Join(", ", Saved.GetValues().Select((c, index) => string.Format("{0} = @p{1}", GetColumnName(c.Key), index + 1))), GetColumnName(Reflection.GetPropertyInfo(KeyField)), OutputInserted ? "INSERTED" : "DELETED", GetColumnName(Reflection.GetPropertyInfo(OutputColumn))); return(context.Database.SqlQuery <T2>(query, Saved.GetValues().Select(c => GetParameter(c.Value)).ToArray()).FirstAsync()); }
public static Task <int> DeleteAsync <T>(this DbContext context, PropertyWatcher <T> Saved) where T : class, new() { string table = GetTableName(typeof(T), context); string query = string.Format("DELETE FROM {0} WHERE {1}", table, string.Join(" AND ", Saved.GetValues().Select((c, index) => string.Format("{0} = @p{1}", GetColumnName(c.Key), index)))); return(context.Database.ExecuteSqlCommandAsync(query, Saved.GetValues().Select(c => GetParameter(c.Value)).ToArray())); }
public void Generic_NoHistory_ChangeOneProperty() { var propWatch = new PropertyWatcher <BasicPOCO>(false); propWatch.Set(c => c.StringValue, "New value!"); var ret = propWatch.GetValues().ToArray(); Assert.AreEqual(1, ret.Length, "Length was incorrect."); Assert.AreEqual("StringValue", ret[0].Key.Name, "Name was incorrect."); Assert.AreEqual("New value!", ret[0].Value, "Value was incorrect."); }
public static async Task UpdateAsync <T>(this DbContext context, IQueryable <T> query, PropertyWatcher <T> Saved) where T : class, new() { //var itemParam = Expression.Parameter(typeof(T), "x"); //var primaryKeys = Utilities.GetPrimaryKeys(typeof(T), context); //var primaryKeyArgs = primaryKeys.Select(c => Expression.PropertyOrField(itemParam, c.Name)).ToArray(); //var selector = Expression.Call(typeof(Tuple), "Create", primaryKeyArgs.Select(c => c.Type).ToArray(), primaryKeyArgs); //var lambda = Expression.Lambda<Func<T, object>>(selector, itemParam); //foreach (var item in query.Select(lambda.Compile())) //{ // T ret = new T(); // foreach (var v in primaryKeys.Select((c, index) => new { c, index })) // { // var tupleProp = item.GetType().GetProperty("Item" + v.index).GetValue(item); // v.c.SetValue(ret, tupleProp); // } // var updated = context.Entry(ret); // Saved.LoadToInstance(ref ret); // foreach (var v in Saved.GetValues()) // { // updated.Property(v.Key.Name).IsModified = true; // } //} await query.ForEachAsync(item => { var ret = item; var updated = context.Entry(ret); Saved.LoadToInstance(ref ret); foreach (var v in Saved.GetValues()) { updated.Property(v.Key.Name).IsModified = true; } }); context.Configuration.ValidateOnSaveEnabled = false; }
public static Task <T> InsertAndFetchAsync <T>(this DbContext context, PropertyWatcher <T> Saved) { string table = GetTableName(typeof(T), context); var vals = Saved.GetValues().ToArray(); if (vals.Length == 0) { string query = string.Format("INSERT INTO {0} OUTPUT INSERTED.* DEFAULT VALUES", table); return(context.Database.SqlQuery <T>(query).FirstAsync()); } else { string query = string.Format("INSERT INTO {0} ({1}) OUTPUT INSERTED.* VALUES ({2})", table, string.Join(", ", vals.Select(c => GetColumnName(c.Key))), string.Join(", ", Enumerable.Range(0, vals.Length).Select(c => "@p" + c))); return(context.Database.SqlQuery <T>(query, vals.Select(c => GetParameter(c.Value)).ToArray()).FirstAsync()); } }
public static void Update <T, T2>(this DbContext context, PropertyWatcher <T> Saved, Expression <Func <T, T2> > KeyField, T2 KeyValue) where T : class, new() { var ret = new T(); Saved.LoadToInstance(ref ret); var prop = Reflection.GetPropertyInfo(KeyField); prop.SetValue(ret, KeyValue); context.Set <T>().Attach(ret); var updated = context.Entry <T>(ret); foreach (var v in Saved.GetValues()) { updated.Property(v.Key.Name).IsModified = true; } context.Configuration.ValidateOnSaveEnabled = false; }
public static Task <T2> InsertAsync <T, T2>(this DbContext context, PropertyWatcher <T> Saved, Expression <Func <T, T2> > OutputColumn) { string table = GetTableName(typeof(T), context); var vals = Saved.GetValues().ToArray(); var outputColumnName = GetColumnName(Reflection.GetPropertyInfo(OutputColumn)); if (vals.Length == 0) { string query = string.Format("INSERT INTO {0} OUTPUT INSERTED.{1} DEFAULT VALUES", table, outputColumnName); return(context.Database.SqlQuery <T2>(query).FirstAsync()); } else { string query = string.Format("INSERT INTO {0} ({1}) OUTPUT INSERTED.{2} VALUES ({3})", table, string.Join(", ", vals.Select(c => GetColumnName(c.Key))), outputColumnName, string.Join(", ", Enumerable.Range(0, vals.Length).Select(c => "@p" + c))); return(context.Database.SqlQuery <T2>(query, vals.Select((c, i) => GetParameter(c.Value)).ToArray()).FirstAsync()); } }