Skip to the content.

DbString causes heap allocations, but achieves the same as DbValueAttribute.

Bad:

public void DapperCode(DbConnection conn)
{
    var sql = "SELECT COUNT(*) FROM Foo WHERE Name = @Name;";
    var cars = conn.Query<int>(sql,
        new
        {
            Name = new DbString
            {
                Value = "MyFoo",
                IsFixedLength = false,
                Length = 5,
                IsAnsi = true
            }
        });
}

Good:

public void DapperCode(DbConnection conn)
{
    var sql = "SELECT COUNT(*) FROM Foo WHERE Name = @Name;";
    var cars = conn.Query<int>(sql,
        new MyPoco
        {
            Name = "MyFoo"
        });
}

class MyPoco
{
	[DbValue(Length = 5, DbType = DbType.AnsiStringFixedLength)] // specify properties here
	public string Name { get; set; }
}