//save collection (with SQL) public static void SaveCollectionSQL <BO>( Dictionary <BO, CommonEnums.DBChangeType> changesDictionary, DbContext context, string insertSQL = null, GetSqlParametersDelegate <BO> insertParamsGetter = null, string updateSQL = null, GetSqlParametersDelegate <BO> updateParamsGetter = null, string deleteSQL = null, GetSqlParametersDelegate <BO> deleteParamsGetter = null) where BO : BaseObject <BO>, new() { DbContextTransaction tran = null; try { tran = context.Database.BeginTransaction(); foreach (KeyValuePair <BO, CommonEnums.DBChangeType> kvp in changesDictionary) { if (kvp.Value == CommonEnums.DBChangeType.New) { ExecuteSqlCommand <BO>( SqlCommandType.Insert, context, kvp.Key, insertSQL, insertParamsGetter); } else if (kvp.Value == CommonEnums.DBChangeType.Dirty) { ExecuteSqlCommand <BO>( SqlCommandType.Update, context, kvp.Key, updateSQL, updateParamsGetter); } else if (kvp.Value == CommonEnums.DBChangeType.Deleted) { ExecuteSqlCommand <BO>( SqlCommandType.Delete, context, kvp.Key, deleteSQL, deleteParamsGetter); } } tran.Commit(); } catch (Exception ex) { tran.Rollback(); ThrowException <BO>(context, "SaveCollectionSQL()", ex); } }
private static int ExecuteSqlCommand <I>(SqlCommandType commandType, DbContext context, I item, string sqlString, GetSqlParametersDelegate <I> paramsGetter) where I : new() { int iRetVal = 0; SqlParameter[] paramsArray = GetSqlParamsArray(paramsGetter(item)); try { iRetVal = context.Database.ExecuteSqlCommand(sqlString, paramsArray); } catch (Exception ex) { ThrowException <I>(context, "ExecuteSqlCommand() Command Type: " + commandType.ToString(), ex); } return(iRetVal); }