Griffin.Data.Mapper.CommandBuilders.CommandBuilder.DeleteCommand C# (CSharp) Method

DeleteCommand() public method

Modifies the command to execute a DELETE statement
/// command /// or /// entity ///
public DeleteCommand ( IDbCommand command, object entity ) : void
command IDbCommand Command that will be executed after this method call
entity object Only primary key properties are used in the WHERE clause
return void
        public void DeleteCommand(IDbCommand command, object entity)
        {
            if (command == null) throw new ArgumentNullException("command");
            if (entity == null) throw new ArgumentNullException("entity");

            var where = "";
            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 += string.Format("{0}=" + "@{1} AND ", property.ColumnName, property.PropertyName);
                command.AddParameter(property.PropertyName, value);
            }

            command.CommandText = string.Format("DELETE FROM {0} WHERE {1}",
                TableName,
                @where.Remove(@where.Length - 5, 5));
        }

Usage Example

        public void do_not_allow_DbNull_in_the_delete_query()
        {
            var mapper = Substitute.For<ICrudEntityMapper>();
            mapper.TableName.Returns("Users");
            mapper.Properties.Returns(new Dictionary<string, IPropertyMapping>
            {
                {"Name", new FakePropertyMapping("Name", "real_name")},
                {"Id", new FakePropertyMapping("Id", "id"){Value = DBNull.Value, IsPrimaryKey = true}}
            });
            var command = new AdoNetFakes.FakeCommand();
            var entity = new { Id = "Hello" };


            var sut = new CommandBuilder(mapper);
            Action actual = () => sut.DeleteCommand(command, entity);

            actual.ShouldThrow<DataException>();
        }
All Usage Examples Of Griffin.Data.Mapper.CommandBuilders.CommandBuilder::DeleteCommand