public void UpdateCommand(IDbCommand command, object entity)
{
if (command == null) throw new ArgumentNullException("command");
if (entity == null) throw new ArgumentNullException("entity");
var updates = "";
var where = "";
foreach (var property in _values)
{
if (!property.CanRead)
continue;
var value = property.GetValue(entity);
updates += string.Format("{0}=@{1}, ", property.ColumnName, property.PropertyName);
command.AddParameter(property.PropertyName, value);
}
if (command.Parameters.Count == 0)
throw new DataException("At least one property (other than primary keys) must be specified.");
foreach (var property in _keys)
{
var value = property.GetValue(entity);
if (value == null || value == DBNull.Value)
throw new DataException(
string.Format("Entity {0}' do not contain a value for the key property '{1}'", entity,
property.PropertyName));
where += property.ColumnName + "=" + "@" + property.PropertyName + " AND ";
command.AddParameter(property.PropertyName, value);
}
if (updates.Length < 2 || where.Length < 5)
throw new DataException(string.Format("Could not construct a proper UPDATE command. Is your mapping for '{0}' correct?\r\n UPDATE clause '{1}'\r\n WHERE clause '{2}'", entity.GetType().FullName, updates, @where));
command.CommandText = string.Format("UPDATE {0} SET {1} WHERE {2}",
TableName,
updates.Remove(updates.Length - 2, 2),
@where.Remove(@where.Length - 5, 5));
}