예제 #1
0
        /// <summary>
        /// Updates an Entity in the database, provided that none of the Primary
        /// keys were modified.
        /// </summary>
        /// <remarks>This method utilizes the <see cref="PreparedNonQuery"/> to speed things along.</remarks>
        /// <param name="obj">The <see cref="TEntity"/> object to update in the dataset</param>
        /// <returns>true if any records in the database were affected; false otherwise.</returns>
        public bool Update(TEntity obj)
        {
            if (UpdateQuery == null)
            {
                using (var updateQuery = new UpdateQueryBuilder(EntityTable.TableName, Context))
                {
                    // Generate the SQL
                    foreach (var attribute in EntityTable.Columns)
                    {
                        // Keys go in the WHERE statement, not the SET statement
                        if (EntityTable.PrimaryKeys.Contains(attribute.Key))
                        {
                            PropertyInfo info = attribute.Value.Property;
                            updateQuery.Where(attribute.Key, Comparison.Equals, new SqlLiteral($"@{attribute.Key}"));
                        }
                        else
                        {
                            updateQuery.Set(attribute.Key, new SqlLiteral($"@{attribute.Key}"));
                        }
                    }

                    UpdateQuery = new PreparedNonQuery(updateQuery.BuildCommand());
                }
            }

            lock (UpdateQuery)
            {
                // Update parameters and execute the SQL Command
                UpdateQuery.SetParameters(obj, EntityTable);
                return(UpdateQuery.Execute() == 1);
            }
        }