Giter Club home page Giter Club logo

dapper's Introduction

Dapper - a simple object mapper for .Net

Build status

Release Notes

Located at https://github.com/DapperLib/Dapper/releases

Packages

MyGet Pre-release feed: https://www.myget.org/gallery/dapper

Package NuGet Stable NuGet Pre-release Downloads MyGet
Dapper Dapper Dapper Dapper Dapper MyGet
Dapper.EntityFramework Dapper.EntityFramework Dapper.EntityFramework Dapper.EntityFramework Dapper.EntityFramework MyGet
Dapper.EntityFramework.StrongName Dapper.EntityFramework.StrongName Dapper.EntityFramework.StrongName Dapper.EntityFramework.StrongName Dapper.EntityFramework.StrongName MyGet
Dapper.Rainbow Dapper.Rainbow Dapper.Rainbow Dapper.Rainbow Dapper.Rainbow MyGet
Dapper.SqlBuilder Dapper.SqlBuilder Dapper.SqlBuilder Dapper.SqlBuilder Dapper.SqlBuilder MyGet
Dapper.StrongName Dapper.StrongName Dapper.StrongName Dapper.StrongName Dapper.StrongName MyGet

Package Purposes:

  • Dapper
    • The core library
  • Dapper.EntityFramework
    • Extension handlers for EntityFramework
  • Dapper.EntityFramework.StrongName
    • Extension handlers for EntityFramework
  • Dapper.Rainbow
    • Micro-ORM implemented on Dapper, provides CRUD helpers (readme)
  • Dapper.SqlBuilder
    • Component for building SQL queries dynamically and composably

Sponsors

Dapper was originally developed for and by Stack Overflow, but is F/OSS. Sponsorship is welcome and invited - see the sponsor link at the top of the page. A huge thanks to everyone (individuals or organisations) who have sponsored Dapper, but a massive thanks in particular to:

Dapper Plus logo

Features

Dapper is a NuGet library that you can add in to your project that will enhance your ADO.NET connections via extension methods on your DbConnection instance. This provides a simple and efficient API for invoking SQL, with support for both synchronous and asynchronous data access, and allows both buffered and non-buffered queries.

It provides multiple helpers, but the key APIs are:

// insert/update/delete etc
var count  = connection.Execute(sql [, args]);

// multi-row query
IEnumerable<T> rows = connection.Query<T>(sql [, args]);

// single-row query ({Single|First}[OrDefault])
T row = connection.QuerySingle<T>(sql [, args]);

where args can be (among other things):

  • a simple POCO (including anonyomous types) for named parameters
  • a Dictionary<string,object>
  • a DynamicParameters instance

Execute a query and map it to a list of typed objects

public class Dog
{
    public int? Age { get; set; }
    public Guid Id { get; set; }
    public string Name { get; set; }
    public float? Weight { get; set; }

    public int IgnoredProperty { get { return 1; } }
}

var guid = Guid.NewGuid();
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });

Assert.Equal(1,dog.Count());
Assert.Null(dog.First().Age);
Assert.Equal(guid, dog.First().Id);

Execute a query and map it to a list of dynamic objects

This method will execute SQL and return a dynamic list.

Example usage:

var rows = connection.Query("select 1 A, 2 B union all select 3, 4").AsList();

Assert.Equal(1, (int)rows[0].A);
Assert.Equal(2, (int)rows[0].B);
Assert.Equal(3, (int)rows[1].A);
Assert.Equal(4, (int)rows[1].B);

Execute a Command that returns no results

Example usage:

var count = connection.Execute(@"
  set nocount on
  create table #t(i int)
  set nocount off
  insert #t
  select @a a union all select @b
  set nocount on
  drop table #t", new {a=1, b=2 });
Assert.Equal(2, count);

Execute a Command multiple times

The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data)

Example usage:

var count = connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
    new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
  );
Assert.Equal(3, count); // 3 rows inserted: "1,1", "2,2" and "3,3"

Another example usage when you already have an existing collection:

var foos = new List<Foo>
{
    { new Foo { A = 1, B = 1 } }
    { new Foo { A = 2, B = 2 } }
    { new Foo { A = 3, B = 3 } }
};

var count = connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)", foos);
Assert.Equal(foos.Count, count);

This works for any parameter that implements IEnumerable<T> for some T.

Performance

A key feature of Dapper is performance. The following metrics show how long it takes to execute a SELECT statement against a DB (in various config, each labeled) and map the data returned to objects.

The benchmarks can be found in Dapper.Tests.Performance (contributions welcome!) and can be run via:

dotnet run --project .\benchmarks\Dapper.Tests.Performance\ -c Release -f net8.0 -- -f * --join

Output from the latest run is:

BenchmarkDotNet v0.13.7, Windows 10 (10.0.19045.3693/22H2/2022Update)
Intel Core i7-3630QM CPU 2.40GHz (Ivy Bridge), 1 CPU, 8 logical and 4 physical cores
.NET SDK 8.0.100
  [Host]   : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX
  ShortRun : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX
ORM Method Return Mean StdDev Error Gen0 Gen1 Gen2 Allocated
Dapper cache impact ExecuteParameters_Cache Void 96.75 us 0.668 us 1.010 us 0.6250 - - 2184 B
Dapper cache impact QueryFirstParameters_Cache Void 96.86 us 0.493 us 0.746 us 0.8750 - - 2824 B
Hand Coded SqlCommand Post 119.70 us 0.706 us 1.067 us 1.3750 1.0000 0.1250 7584 B
Hand Coded DataTable dynamic 126.64 us 1.239 us 1.873 us 3.0000 - - 9576 B
SqlMarshal SqlCommand Post 132.36 us 1.008 us 1.523 us 2.0000 1.0000 0.2500 11529 B
Dapper QueryFirstOrDefault Post 133.73 us 1.301 us 2.186 us 1.7500 1.5000 - 11608 B
Mighty Query dynamic 133.92 us 1.075 us 1.806 us 2.0000 1.7500 - 12710 B
LINQ to DB Query Post 134.24 us 1.068 us 1.614 us 1.7500 1.2500 - 10904 B
RepoDB ExecuteQuery Post 135.83 us 1.839 us 3.091 us 1.7500 1.5000 - 11649 B
Dapper 'Query (buffered)' Post 136.14 us 1.755 us 2.653 us 2.0000 1.5000 - 11888 B
Mighty Query Post 137.96 us 1.485 us 2.244 us 2.2500 1.2500 - 12201 B
Dapper QueryFirstOrDefault dynamic 139.04 us 1.507 us 2.279 us 3.5000 - - 11648 B
Mighty SingleFromQuery dynamic 139.74 us 2.521 us 3.811 us 2.0000 1.7500 - 12710 B
Dapper 'Query (buffered)' dynamic 140.13 us 1.382 us 2.090 us 2.0000 1.5000 - 11968 B
ServiceStack SingleById Post 140.76 us 1.147 us 2.192 us 2.5000 1.2500 0.2500 15248 B
Dapper 'Contrib Get' Post 141.09 us 1.394 us 2.108 us 2.0000 1.5000 - 12440 B
Mighty SingleFromQuery Post 141.17 us 1.941 us 2.935 us 1.7500 1.5000 - 12201 B
Massive 'Query (dynamic)' dynamic 142.01 us 4.957 us 7.494 us 2.0000 1.5000 - 12342 B
LINQ to DB 'First (Compiled)' Post 144.59 us 1.295 us 1.958 us 1.7500 1.5000 - 12128 B
RepoDB QueryField Post 148.31 us 1.742 us 2.633 us 2.0000 1.5000 0.5000 13938 B
Norm 'Read<> (tuples)' ValueTuple`8 148.58 us 2.172 us 3.283 us 2.0000 1.7500 - 12745 B
Norm 'Read<()> (named tuples)' ValueTuple`8 150.60 us 0.658 us 1.106 us 2.2500 2.0000 1.2500 14562 B
RepoDB Query Post 152.34 us 2.164 us 3.271 us 2.2500 1.5000 0.2500 14106 B
RepoDB QueryDynamic Post 154.15 us 4.108 us 6.210 us 2.2500 1.7500 0.5000 13930 B
RepoDB QueryWhere Post 155.90 us 1.953 us 3.282 us 2.5000 0.5000 - 14858 B
Dapper cache impact ExecuteNoParameters_NoCache Void 162.35 us 1.584 us 2.394 us - - - 760 B
Dapper cache impact ExecuteNoParameters_Cache Void 162.42 us 2.740 us 4.142 us - - - 760 B
Dapper cache impact QueryFirstNoParameters_Cache Void 164.35 us 1.206 us 1.824 us 0.2500 - - 1520 B
DevExpress.XPO FindObject Post 165.87 us 1.012 us 1.934 us 8.5000 - - 28099 B
Dapper cache impact QueryFirstNoParameters_NoCache Void 173.87 us 1.178 us 1.781 us 0.5000 - - 1576 B
LINQ to DB First Post 175.21 us 2.292 us 3.851 us 2.0000 0.5000 - 14041 B
EF 6 SqlQuery Post 175.36 us 2.259 us 3.415 us 4.0000 0.7500 - 24209 B
Norm 'Read<> (class)' Post 186.37 us 1.305 us 2.496 us 3.0000 0.5000 - 17579 B
DevExpress.XPO GetObjectByKey Post 186.78 us 3.407 us 5.151 us 4.5000 1.0000 - 30114 B
Dapper 'Query (unbuffered)' dynamic 194.62 us 1.335 us 2.019 us 1.7500 1.5000 - 12048 B
Dapper 'Query (unbuffered)' Post 195.01 us 0.888 us 1.343 us 2.0000 1.5000 - 12008 B
DevExpress.XPO Query Post 199.46 us 5.500 us 9.243 us 10.0000 - - 32083 B
Belgrade FirstOrDefault Task`1 228.70 us 2.181 us 3.665 us 4.5000 0.5000 - 20555 B
EF Core 'First (Compiled)' Post 265.45 us 17.745 us 26.828 us 2.0000 - - 7521 B
NHibernate Get Post 276.02 us 8.029 us 12.139 us 6.5000 1.0000 - 29885 B
NHibernate HQL Post 277.74 us 13.032 us 19.703 us 8.0000 1.0000 - 31886 B
NHibernate Criteria Post 300.22 us 14.908 us 28.504 us 13.0000 1.0000 - 57562 B
EF 6 First Post 310.55 us 27.254 us 45.799 us 13.0000 - - 43309 B
EF Core First Post 317.12 us 1.354 us 2.046 us 3.5000 - - 11306 B
EF Core SqlQuery Post 322.34 us 23.990 us 40.314 us 5.0000 - - 18195 B
NHibernate SQL Post 325.54 us 3.937 us 7.527 us 22.0000 1.0000 - 80007 B
EF 6 'First (No Tracking)' Post 331.14 us 27.760 us 46.649 us 12.0000 1.0000 - 50237 B
EF Core 'First (No Tracking)' Post 337.82 us 27.814 us 46.740 us 3.0000 1.0000 - 17986 B
NHibernate LINQ Post 604.74 us 5.549 us 10.610 us 10.0000 - - 46061 B
Dapper cache impact ExecuteParameters_NoCache Void 623.42 us 3.978 us 6.684 us 3.0000 2.0000 - 10001 B
Dapper cache impact QueryFirstParameters_NoCache Void 630.77 us 3.027 us 4.576 us 3.0000 2.0000 - 10640 B

Feel free to submit patches that include other ORMs - when running benchmarks, be sure to compile in Release and not attach a debugger (Ctrl+F5).

Alternatively, you might prefer Frans Bouma's RawDataAccessBencher test suite or OrmBenchmark.

Parameterized queries

Parameters are usually passed in as anonymous classes. This allows you to name your parameters easily and gives you the ability to simply cut-and-paste SQL snippets and run them in your db platform's Query analyzer.

new {A = 1, B = "b"} // A will be mapped to the param @A, B to the param @B

Parameters can also be built up dynamically using the DynamicParameters class. This allows for building a dynamic SQL statement while still using parameters for safety and performance.

    var sqlPredicates = new List<string>();
    var queryParams = new DynamicParameters();
    if (boolExpression)
    {
        sqlPredicates.Add("column1 = @param1");
        queryParams.Add("param1", dynamicValue1, System.Data.DbType.Guid);
    } else {
        sqlPredicates.Add("column2 = @param2");
        queryParams.Add("param2", dynamicValue2, System.Data.DbType.String);
    }

DynamicParameters also supports copying multiple parameters from existing objects of different types.

    var queryParams = new DynamicParameters(objectOfType1);
    queryParams.AddDynamicParams(objectOfType2);

When an object that implements the IDynamicParameters interface passed into Execute or Query functions, parameter values will be extracted via this interface. Obviously, the most likely object class to use for this purpose would be the built-in DynamicParameters class.

List Support

Dapper allows you to pass in IEnumerable<int> and will automatically parameterize your query.

For example:

connection.Query<int>("select * from (select 1 as Id union all select 2 union all select 3) as X where Id in @Ids", new { Ids = new int[] { 1, 2, 3 } });

Will be translated to:

select * from (select 1 as Id union all select 2 union all select 3) as X where Id in (@Ids1, @Ids2, @Ids3)" // @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3

Literal replacements

Dapper supports literal replacements for bool and numeric types.

connection.Query("select * from User where UserTypeId = {=Admin}", new { UserTypeId.Admin });

The literal replacement is not sent as a parameter; this allows better plans and filtered index usage but should usually be used sparingly and after testing. This feature is particularly useful when the value being injected is actually a fixed value (for example, a fixed "category id", "status code" or "region" that is specific to the query). For live data where you are considering literals, you might also want to consider and test provider-specific query hints like OPTIMIZE FOR UNKNOWN with regular parameters.

Buffered vs Unbuffered readers

Dapper's default behavior is to execute your SQL and buffer the entire reader on return. This is ideal in most cases as it minimizes shared locks in the db and cuts down on db network time.

However when executing huge queries you may need to minimize memory footprint and only load objects as needed. To do so pass, buffered: false into the Query method.

Multi Mapping

Dapper allows you to map a single row to multiple objects. This is a key feature if you want to avoid extraneous querying and eager load associations.

Example:

Consider 2 classes: Post and User

class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public User Owner { get; set; }
}

class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Now let us say that we want to map a query that joins both the posts and the users table. Until now if we needed to combine the result of 2 queries, we'd need a new object to express it but it makes more sense in this case to put the User object inside the Post object.

This is the use case for multi mapping. You tell dapper that the query returns a Post and a User object and then give it a function describing what you want to do with each of the rows containing both a Post and a User object. In our case, we want to take the user object and put it inside the post object. So we write the function:

(post, user) => { post.Owner = user; return post; }

The 3 type arguments to the Query method specify what objects dapper should use to deserialize the row and what is going to be returned. We're going to interpret both rows as a combination of Post and User and we're returning back a Post object. Hence the type declaration becomes

<Post, User, Post>

Everything put together, looks like this:

var sql =
@"select * from #Posts p
left join #Users u on u.Id = p.OwnerId
Order by p.Id";

var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});
var post = data.First();

Assert.Equal("Sams Post1", post.Content);
Assert.Equal(1, post.Id);
Assert.Equal("Sam", post.Owner.Name);
Assert.Equal(99, post.Owner.Id);

Dapper is able to split the returned row by making an assumption that your Id columns are named Id or id. If your primary key is different or you would like to split the row at a point other than Id, use the optional splitOn parameter.

Multiple Results

Dapper allows you to process multiple result grids in a single query.

Example:

var sql =
@"
select * from Customers where CustomerId = @id
select * from Orders where CustomerId = @id
select * from Returns where CustomerId = @id";

using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
{
   var customer = multi.Read<Customer>().Single();
   var orders = multi.Read<Order>().ToList();
   var returns = multi.Read<Return>().ToList();
   ...
}

Stored Procedures

Dapper fully supports stored procs:

var user = cnn.Query<User>("spGetUser", new {Id = 1},
        commandType: CommandType.StoredProcedure).SingleOrDefault();

If you want something more fancy, you can do:

var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

cnn.Execute("spMagicProc", p, commandType: CommandType.StoredProcedure);

int b = p.Get<int>("@b");
int c = p.Get<int>("@c");

Ansi Strings and varchar

Dapper supports varchar params, if you are executing a where clause on a varchar column using a param be sure to pass it in this way:

Query<Thing>("select * from Thing where Name = @Name", new {Name = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true }});

On SQL Server it is crucial to use the unicode when querying unicode and ANSI when querying non unicode.

Type Switching Per Row

Usually you'll want to treat all rows from a given table as the same data type. However, there are some circumstances where it's useful to be able to parse different rows as different data types. This is where IDataReader.GetRowParser comes in handy.

Imagine you have a database table named "Shapes" with the columns: Id, Type, and Data, and you want to parse its rows into Circle, Square, or Triangle objects based on the value of the Type column.

var shapes = new List<IShape>();
using (var reader = connection.ExecuteReader("select * from Shapes"))
{
    // Generate a row parser for each type you expect.
    // The generic type <IShape> is what the parser will return.
    // The argument (typeof(*)) is the concrete type to parse.
    var circleParser = reader.GetRowParser<IShape>(typeof(Circle));
    var squareParser = reader.GetRowParser<IShape>(typeof(Square));
    var triangleParser = reader.GetRowParser<IShape>(typeof(Triangle));

    var typeColumnIndex = reader.GetOrdinal("Type");

    while (reader.Read())
    {
        IShape shape;
        var type = (ShapeType)reader.GetInt32(typeColumnIndex);
        switch (type)
        {
            case ShapeType.Circle:
            	shape = circleParser(reader);
            	break;
            case ShapeType.Square:
            	shape = squareParser(reader);
            	break;
            case ShapeType.Triangle:
            	shape = triangleParser(reader);
            	break;
            default:
            	throw new NotImplementedException();
        }

      	shapes.Add(shape);
    }
}

User Defined Variables in MySQL

In order to use Non-parameter SQL variables with MySql Connector, you have to add the following option to your connection string:

Allow User Variables=True

Make sure you don't provide Dapper with a property to map.

Limitations and caveats

Dapper caches information about every query it runs, this allows it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object. Statements that are only used once are routinely flushed from this cache. Still, if you are generating SQL strings on the fly without using parameters it is possible you may hit memory issues.

Dapper's simplicity means that many features that ORMs ship with are stripped out. It worries about the 95% scenario, and gives you the tools you need most of the time. It doesn't attempt to solve every problem.

Will Dapper work with my DB provider?

Dapper has no DB specific implementation details, it works across all .NET ADO providers including SQLite, SQL CE, Firebird, Oracle, MySQL, PostgreSQL and SQL Server.

Do you have a comprehensive list of examples?

Dapper has a comprehensive test suite in the test project.

Who is using this?

Dapper is in production use at Stack Overflow.

dapper's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dapper's Issues

Allow enumerable to be passed as a TVP

This is not an issue, but since I have no clue how to submit an enhancement, here I go ...

I'd like to submit for your consideration the following Extension to Dapper:

/// <summary>
/// This extension converts an enumerable set to a Dapper TVP
/// </summary>
/// <typeparam name="T">type of enumerbale</typeparam>
/// <param name="enumerable">list of values</param>
/// <param name="typeName">database type name</param>
/// <param name="orderedColumnNames">if more than one column in a TVP, columns order must mtach order of columns in TVP</param>
/// <returns>a custom query parameter</returns>
public static SqlMapper.ICustomQueryParameter AsTableValuedParameter<T>(this IEnumerable<T> enumerable,
    string typeName, IEnumerable<string> orderedColumnNames = null)
{
    var dataTable = new DataTable();
    if (typeof(T).IsValueType)
    {
        dataTable.Columns.Add("NONAME", typeof(T));
        foreach (T obj in enumerable)
        {
            dataTable.Rows.Add(obj);
        }                
    }
    else
    {
        PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        PropertyInfo[] readableProperties = properties.Where(w => w.CanRead).ToArray();
        if (readableProperties.Length > 1 && orderedColumnNames == null)
            throw new ArgumentException("Ordered list of column names must be provided when TVP contains more than one column");
        var columnNames = (orderedColumnNames ?? readableProperties.Select(s => s.Name)).ToArray();
        foreach (string name in columnNames)
        {
            dataTable.Columns.Add(name, readableProperties.Single(s => s.Name.Equals(name)).PropertyType);
        }

        foreach (T obj in enumerable)
        {
            dataTable.Rows.Add(
                columnNames.Select(s => readableProperties.Single(s2 => s2.Name.Equals(s)).GetValue(obj))
                    .ToArray());
        }
    }
    return dataTable.AsTableValuedParameter(typeName);
}

Sample uses:

var l = new List<AUDITRECORD>
{
    new AUDITRECORD {AUDIT_PK = 2, MESSAGE = "HELO", SUCCESS = true},
    new AUDITRECORD {AUDIT_PK = 3, MESSAGE = "EHLO", SUCCESS = false}
};

using (var conn = new SqlConnection(connString))
{
    conn.Open();
    conn.Execute("TestOne",
        new { TVP = l.AsTableValuedParameter("AuMine.AUDITRECORD", new[] { "AUDIT_PK", "MESSAGE", "SUCCESS" }) },
        commandType: CommandType.StoredProcedure);
}

var customerKeys = new List<long>{3,5,7};
var productKeys = new List<long>{11,33,55};

using (var conn = new SqlConnection("Data Source=.;Initial Catalog=Scratch;Integrated Security=true"))
{
    conn.Open();
    conn.Execute("TestTwo",
        new
        {
            customerKeys = customerKeys.AsTableValuedParameter("dbo.TVPBIGINT"),
            productKeys = productKeys.AsTableValuedParameter("dbo.TVPBIGINT")
        },
        commandType: CommandType.StoredProcedure);
}

Bad SQL generated for Oracle and empty IEnumerable

Dapper generates SQL for Oracle when passing empty IEnumerables to an IN() clause:

var param = new List<string>();
param.Add("PS"); // Add one or more entries
var bar = Database.Query<mytable>(ConnectInfo,
    @"SELECT * FROM mytable WHERE myvarchar2 IN :param", new {param});

...executes without issue, but:

var param = new List<string>();
// param.Add("PS");
var bar = Database.Query<mytable>(ConnectInfo,
    @"SELECT * FROM mytable WHERE myvarchar2 IN :param", new {param});

Fails with: ORA-00923: FROM keyword not found where expected

See my SO question for more details:
http://stackoverflow.com/questions/26877297/dapper-sql-generation-bug-oracle-and-the-sqls-in-clause

IEnumerable<string> does not respect TypeMap

Setting the typemap to map string to AnsiString works with a single parameter. When passing in an IEnumerable it automatically uses String instead of looking up the DBType in the typemap.

How to log dynamic parameters?

When a dapper sql command fails, I want to log the command along with the dynamic parameters. Something like

private static void LogCommand(string cmd, DynamicParameters dp)
{
var dpl = dp as SqlMapper.IParameterLookup;
Debug.WriteLine(cmd);
Debug.WriteLine("Parameters: " +
String.Join(", ",
from pn in dp.ParameterNames
select string.Format("@{0}={1}", pn, dpl[pn])));
}

Is this a good approach? Any alternative, maybe built in logging support?

TVP SqlDataRecord vs DataTable

One of the projects I have been maintaining occasionally ends up using a large lists of IDs which then get turned into large amount of parameters. As such, I've been doing some initial poking around to see if it was worth switching over to TVPs instead of just using the 'IN' support.

What I found is that after a ~75 parameters or so TVPs become faster. As the numbers grow, it becomes a massive difference; To the point where once you get to 1000 you being talking about orders of magnitude.

What I also found is that in terms of execution speed there isn't very much difference. DataTables might be very slightly slower, but it's within the margin of error. However, the IEnumerable<int> into a DataTable takes much longer than it does to an List<SqlDataRecord>. With a test of 1000 ints, it takes twice as long to make the DataTable as it does the List. (Ignoring some 100% reproducible weirdness, where generating the DataTable takes 50x as long in one step of the benchmark. However, if I copy paste that step, then the copy pasted section runs without speed again. This 50x delay makes it almost as long as the 'IN parameter' dapper code. I thought it might be GC, but I am forcing it do collection between steps, and it doesn't seem to help.)

This generation speed seems to hold true for the current Dapper TVP support. If I create class inheriting IDynamicParameters, and use it to generate and pass a List<SqlDataRecord> to Dapper, it runs faster than just passing the DataTable as a parameter. The gap in speed aligns pretty well with the difference in generation methods I measured in the hand coded tests. However, the method I am using to do this restricts me to only sending the one parameter, so it's obviously not ideal.

So the question is, before I start trying to schedule time in the future to dig into the core of Dapper. Is there technical reason that makes supporting List<SqlDataRecord> difficult?

TypeHandler<> can't override default typeMap for base types

Trying to solve an issue (losing milliseconds on DateTime in SQLite database) using a TypeHandler that do conversion between db string (in format "o") and DateTime doesn't pass test because typeMap[DateTime] is processed before custom type handlers.

Probably the solution is to invert the lookup sequence or add the chance for remove a default type map with a brother of

public static void AddTypeMap(Type type, DbType dbType)

as

public static void RemoveTypeMap(Type type)

Support for output parameter member expressions

It would be cool if you could specify an instance and a member expression for output parameters in the DynamicParameters bag. This would be useful for populating object properties after operations.

How it is:

var sql = @"
INSERT INTO dbo.posts VALUES @AuthorId, @Title, @Body
SET @PostId = SCOPE_IDENTITY()
SET @Author = (SELECT Name FROM dbo.users WHERE Id = @AuthorId)
";

var param = new DynamicParameters(post);
param.Add("@PostId", dbType: DbType.Int32, direction: ParameterDirection.Output);
param.Add("@Author", dbType: DbType.String, direction: ParameterDirection.Output);

GetConnection().Execute(sql, param);

post.PostId = param.Get<int>("@PostId");
post.Author = param.Get<string>("@Author");

How it could be:

var sql = @"
INSERT INTO dbo.posts VALUES @AuthorId, @Title, @Body
SET @PostId = SCOPE_IDENTITY()
SET @Author = (SELECT Name FROM dbo.users WHERE Id = @AuthorId)
";

var param = new DynamicParameters(post)
    .Output(post, p => p.PostId)
    .Output(post, p => p.Author);

GetConnection().Execute(sql, param);

ORA-00911:invalid character๏ผŒif parameter startwith underscore

string sql = @"UPDATE mytalbe SET Name=:name WHERE ID=:_id";
var r = conn.Execute(sql, new { _id=123,name="test"}

run this code,throw exception:ORA-00911:invalid character

I found ,change _id to id,it is work fine.

BTW:I useOracle Data Provider for .NET (ODP.NET) Managed Driver 121.1.0

Execute() not finding public fields on the input "param" object

I'm emitting a class at runtime whose purpose is to represent a data object out of a database.

For simplicity, it's emitted as a class with a bunch of public fields.

If I pass an instance of this class to Execute() as the "param" argument, it does not recognize the public fields and map them to my sql variables.

I am having to re-emit them as public properties as a workaround.

How to convert dynamic list to strong type object list

Hi
I am developing one application using dapper dot net, need to get collection of objects where columns are configurable, when i tried ienumerable dynamic list, i am getting list of Key Value Pair of records
But i need list of records with columns (as we get in strong type ienumerable collection)

I tried below code:

IEnumerable tickets = null;
using (var dbcon = new DBContext())
{
tickets = dbcon.Connection.Query(" Select top 100 Ticket_ID,Ticket_No,Status,Reg_Time from IM_TicketMaster where sup_Function=@instance", new { Instance = new DbString { Value = "IT" } });
}
the result is IEnumerable collection of DapperRow, when i convert this collection to json, it gives me a collection of key value pair, how do i convert this dynamic list to strong type collection.

Is there any proper way to get List of dynamic json objects instead of key value collection?

Null value does not invoke property setter

This may be by design, but I am finding that if a returned value from the database is null, the property setter on the object is not being invoked. For my use case (ActiveRecord-style objects) this is causing some issues when I want to have something like the following:

public class Ticket
{
    public int? AssignedTo
    {
        get
        {
            return _currentValue;
        }
        set 
        {
            if (!_init)
            {
                _initialValue= value;
                _init = true;
            }

            _currentValue = value;
        }
    }
}

If I run a query like so...

var sql = @"
SELECT t.*, a.UserId AS AssignedTo
FROM dbo.Tickets t
LEFT JOIN dbo.vw_MostRecentAssignees a
    ON a.TicketId = t.TicketId
WHERE t.TicketId = @ticketId
";

var ticket = conn.Query<Ticket>(sql, new { ticketId }).SingleOrDefault();

...and that particular ticket has no current assignee, then I can't track changes to that property.

I can do something like this:

var sql = @"
SELECT t.*, 1 AS splitHere, a.UserId AS AssignedTo
FROM dbo.Tickets t
LEFT JOIN dbo.vw_MostRecentAssignees a
    ON a.TicketId = t.TicketId
WHERE t.TicketId = @ticketId
";

var ticket = conn.Query<Ticket, dynamic, Ticket>(sql, (t, d) =>
{
    t.AssignedTo = d.AssignedTo;
    return t;
}, new { ticketId }, splitOn: "splitHere").SingleOrDefault();

...but that's not very pretty. ;)

ora-00936 error with Oracle

If I use dapper to execute query like below:

var result = con.Query("SELECT * FROM table WHERE col = @col", new { col = "123" });

I get ora-00936 exception but if I do like below it works fine:

var result = con.Query(String.Format("SELECT * FROM table WHERE col = '{0}'", "123"));

Any ideas to why?

Everything needs to be public and virtual!!!

Please make all methods and classes public and all methods marked with virtual so I can extend them easily. There are tools you can use to do this automatically for you at compile time I believe. This is an API and I don't want to compile my own version so I can get to the definition of Dapper.SqlMapper.DapperRow for instance. There isn't any reason to make things private since all the source code is open source.

SqlMapperExtensions.Update ignores ComputedAttribute

What steps will reproduce the problem?

  1. Create a test class and the corresponding table.
  2. Create a new property (not presented in the table) and decorate it with [ComputedAttribute].
  3. Create and update an instance of the test class.

What is the expected output? The corresponding database row should be updated.
What do you see instead? An error message saying that the property created in step 2, cannot be updated.

What version of the product are you using? - latest one from May 2014.
On what operating system? Windows 7.

Please provide any additional information below.
Please fix the line 243 of SqlMapperExtensions.cs:
from
var nonIdProps = allProperties.Where(a => !keyProperties.Contains(a));
to
var computedProperties = ComputedPropertiesCache(type);
var nonIdProps = allProperties.Except(keyProperties.Union(computedProperties));

Can't build the solution - Dapper NET45

Hello,

I just get the last sources, and I'am not able to build the solution:

The error is (Dapper NET45, SqlMapperAsync.cs):
"Cannot await in the body of a finally clause", line 575.

Because it's apparently not possible to call the following code from the finally block:
if (index == gridIndex)
{
await NextResultAsync().ConfigureAwait(false);
}

Could you please fix this issue?

Thanks,
Thierry M.

How to turn Arrays support off in v1.31

The ability to turn off arrays support has been removed on version 1.31

FeatureSupport.Get(cn).Arrays = false;
This doesn't work anymore since the property can only be set privately. I wonder the reason behind this.
There are some queries in PostGreSQL in which it is convenient to turn array support off.

Thanks.
Diego

error message not helpful when type mismatch

public class Person { public string Id { get; set; } /* etc... */ }
var result = cnn.Query(@"select top 1 * from Persons").ToList().First();

if Id is defined in the DB as uniqueidentifier, then the error message reads:
Error parsing column 0 (Owner=Invalid attempt to read from column ordinal '0'. With CommandBehavior.SequentialAccess, you may only read from column ordinal '1' or greater.)

IMO this should just work and populate the string Id (this would be consistent with how it works when saving - you can write a string into an uniqueidentifier column)

ParamReader crashed when use a struct object as query parameter

Hi~

I've encountered a crash when trying to read parameter value from a struct object.
A test case would be like:

struct Car { /* from Tests\Tests.cs */ }

public void TestStructAsParam()
{
    var car1 = new Car() { Name = "Ford", Age = 21, Trap = Car.TrapEnum.B };
    var car2 = connection.Query<Car>("select @Name Name, @Age Age, @Trap Trap", car1).First();

    car2.Name.IsEqualTo(car1.Name);
    car2.Age.IsEqualTo(car1.Age);
    car2.Trap.IsEqualTo(car1.Trap);
}

The generated ParamReader will throw a System.InvalidProgramException while setting up command.

System.InvalidProgramException: Common Language Runtime detected an invalid program.

StackTrace:
    ParamInfo47fc3058-e009-477d-8e7e-64bc77e90be6(IDbCommand , Object )
    Dapper.CommandDefinition.SetupCommand(IDbConnection cnn, Action`2 paramReader)
    Dapper.SqlMapper.<QueryImpl>d__11`1.MoveNext()

BUG: Insert fails with "No mapping exists from DbType Object to a known SqlDbType" exception when entity has a dynamic Id field

Hi,

Issue could be related to #130 and affects recent version of Dapper.

To reproduce:

// Entity has dynamic Id
public class Dyno
{
    public dynamic Id { get; set; }
    public string Name { get; set; }
}

// Test case
[Fact]
public void Insert_EntityWithDynamicGuidId_Ok()
{
    using (var conn = GetConnection())
    {
        conn.Open();

        try
        {
            conn.Execute("drop table [Dyno];");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        conn.Execute("create table [Dyno] ([Id] uniqueidentifier primary key, [Name] nvarchar(50) not null);");

        var result = conn.Execute("insert into [Dyno] ([Id], [Name]) values (@Id, @Name);", new Dyno { Name = "T Rex", Id = Guid.NewGuid() }, commandType: CommandType.Text);

        Assert.Equal(1, result);

        conn.Close();
        conn.Dispose();
    }
}

Interestingly, this works if I pass in an anonymous type parameter, ie:

var result = conn.Execute("insert into [Dyno] ([Id], [Name]) values (@Id, @Name);", new { Name = "T Rex", Id = Guid.NewGuid() }, commandType: CommandType.Text);

Dapper.Contrib Insert Fails - Format Query

Dapper.Contrib.Insert generates a query wrong.
Example:
{insert into Markers (Name, Address,) values โ€‹โ€‹(@ Name, @ Address,}

This error occurs when the key (Id) is the last in the list allProperties.
var allProperties = TypePropertiesCache(type);
The current code only works correctly when the key (Id) is the first in the list (allProperties).

I modified the code to work regardless of the order of the properties, I hope will be welcome:)

public static long Insert(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var type = typeof(T);

        var name = GetTableName(type);

        var sb = new StringBuilder(null);
        sb.AppendFormat("insert into {0} (", name);

        var allProperties = TypePropertiesCache(type);
        var keyProperties = KeyPropertiesCache(type);
        var allPropertiesExceptKey = allProperties.Except(keyProperties);

        for (var i = 0; i < allPropertiesExceptKey.Count(); i++)
        {
            var property = allPropertiesExceptKey.ElementAt(i);
            sb.Append(property.Name);
            if (i < allPropertiesExceptKey.Count() - 1)
                sb.Append(", ");
        }
        sb.Append(") values (");

        for (var i = 0; i < allPropertiesExceptKey.Count(); i++)
        {
            var property = allPropertiesExceptKey.ElementAt(i);
            sb.AppendFormat("@{0}", property.Name);
            if (i < allPropertiesExceptKey.Count() - 1)
                sb.Append(", ");
        }
        sb.Append(") ");
        connection.Execute(sb.ToString(), entityToInsert, transaction: transaction, commandTimeout: commandTimeout);
        //NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
        var r = connection.Query("select @@IDENTITY id", transaction: transaction, commandTimeout: commandTimeout);
        return (int)r.First().id;
    }

Update Dapper.Contrib NuGet distribution to a version that works without Transactions

The current version of Dapper.Contrib published to NuGet (1.0) requires a transaction which doesn't match the documentation. When used normally, you will get the following error on insert:

System.InvalidOperationExceptionExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.

Please update NuGet with the latest version which fixes this issue.

Getting an exception, because of closed dbreader

Hi!
I'm getting an exception of type "System.InvalidOperationException" with message "Invalid attempt to call FieldCount when reader is closed."

Additional information:
Source: "System.Data"
StackTrace:
at System.Data.SqlClient.SqlDataReader.get_FieldCount()
at Dapper.SqlMapper.GetColumnHash(IDataReader reader) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 468
at Dapper.SqlMapper.d__148.MoveNext() in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1719 at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at Implementation.UserRepository.d__1b.MoveNext() in UserRepository.cs:line 217

It's reproducing only on async queries with buffered parameter set to false. If I set true, every thing is fine.

UPPER keyword causes column to be null

If I add UPPER keyword around a column and query with Dapper that column comes back null, without it isn't null.

select id, UPPER(column) from T - Column is null on result set.
select id, column from T - Column is not null on result set.

In the same vein of TypeHandlers, how about a QueryProcessor?

I'm currently trying to decide whether or not to fork and modify Dapper to accomplish something that isn't really possible now and that is to be able to modify the command text of a query.

The situation: we have a fairly large existing code base that uses Dapper and we need to have the ability to modify our queries to be context dependent. That is, if a query is currently select * from Products where ID = @id we want to be able to have that (depending on the current context) get transformed into select * from Products_en where ID = @id when the current user's language is set to English. First of, no that can't be accomplished by adding a language filter in the where-clause since the context I'm using here is greatly simplified, they really are physically different tables :) I think we really need a dynamic query in our case.

It's inevitable that we'll have to modify each and every query that currently touches the Products table; we'll have to replace every instance of from Products with something like from [[PRODUCTS]]. However, while we could do the actual context dependent replace inside something like connection.Query<int>(QueryProcessor.Process("select count(*) from [[PRODUCTS]]")) I prefer not having to do this for each query as it's something that can be easily forgotten when writing a query and it would be a maintenance nightmare if we ever to decide to change the method signature, having to modify 300 references to that method.

So that's where modifying Dapper comes in: would it be a good idea to add a SqlMapper.AddCommandTextProcessor(ICommandTextProcessor processor)? With ICommandTextProcessor being defined as:

public interface ICommandTextProcessor
{
  string Process(string commandText);
}

If one has been defined, it would then be used inside the CommandDefinition type's constructor to do something with the query. In our case, it would be replacing the placeholders with a proper implementation.

Of course, if I make this modification it would be preferable if it was one that could be merged back to the mainline :) So I'm open to any suggestions here.

Table Valued parameters to Stored procedure fails

Upgraded Dapper from 1.26 to 1.34. Am getting this error wherever I pass Table Valued parameters to a stored procedure (using SQL Azure)

The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 7 : Data type 0x62 (sql_variant) has an invalid type for type-specific metadata.

What have I missed out on??

NullReferenceException when using custom type handlers for value types

Problem

If you register a custom type handler for a value type but do not register a type handler for the nullable version then a NullReferenceException can be thrown.

The NullReferenceException is thrown when the nullable custom type is used as a parameter value, and the parameter is not null.

The NullReferenceException is thrown from TypeHandlerCache<T>.SetValue because no handler has been registered for the nullable version.

Expected Behaviour

A type handler registered for a value type should support nulls automatically.

Details

The application has a value type LocalDate that has been registered using the following.

SqlMapper.AddTypeHandler(typeof(LocalDate), LocalDateHandler.Default)

See https://gist.github.com/chilversc/994509807c177259e77b for a complete example.


CreateParamInfoGenerator line 2580

DbType dbType = LookupDbType(prop.PropertyType, prop.Name, out handler);

Assume that the property type is Nullable<LocalDate> and the value of the property is not null.


LookupDbType line 716

if (typeHandlers.TryGetValue(type, out handler))

The parameter type has been changed from Nullable<LocalDate> to LocalDate.
This finds the handler for LocalDate that was registered.


CreateParamInfoGenerator line 2683

if (handler != null)
{
    il.Emit(OpCodes.Call, typeof(TypeHandlerCache<>).MakeGenericType(prop.PropertyType).GetMethod("SetValue")); // stack is now [parameters] [[parameters]] [parameter]
}

The test passes and IL is generated to call TypeHandlerCache<Nullable<LocalDate>>.SetValue

Later, when issuing a query that has a non-null, Nullable<LocalDate> the application will throw a NullReferenceException from TypeHandlerCache.SetValue line 667 because no handler has been registered for Nullable<LocalDate>.

The handler that was found was for TypeHandlerCache<LocalDate>.SetValue

Solutions

Parity between SetValue and Parse

There is a disparity between the SetValue and Parse methods of ITypeHandler. Parse is never called with a null value while SetValue is. Ideally I think these methods should have parity, and that SetValue should not be called if the value is null. Instead there should be a null check before calling SetValue.

This will break existing code that is performing custom logic if the value is null, instead of just setting the parameter to DBNull.

Automatically register a nullable handler.

When calling AddTypeHandler, if the type is a value type automatically register the nullable version of the type.

A breaking change is that if a client manually registers a different nullable custom type handler before registering a non-nullable handler the nullable handler will be replaced.

There is no issue with a custom handler's SetValue being unexpectedly called with a value of DBNull because the application would have crashed with a NullReferenceException requiring the developer to either use a non-nullable parameter or register a nullable custom type handler.

There is a small edge case if a client calls AddTypeHandler(typeof(LocalDate), null). SqlMapper will remove both the nullable and non-nullable handlers, even if the nullable handler was separately registered. I think this is a rare case and unlikely to break existing clients.

Improve exception message

Keep the behaviour as is, but detect if handler ais null and the type is nullable. When this condition occurs throw a more detailed error message including a hint to register the nullable handler.

This has the least impact on clients by not changing the behaviour but makes it much easier for the developer to understand what has gone wrong and fix it.

Recommendation

I think automatically registering a nullable type handler is the best solution. It has minimal impact on existing clients while matching the expected behaviour.

The issue with overwriting an existing nullable handler is small. Having different nullable and non-nullable handlers for the same type isn't really supported as there are several places where the non-nullable version will be used even if a nullable version is registered, such as DynamicParameters.AddParameters line 4043.

Workaround

Register the custom handler twice, once for the non-nullable version, and once for the non-nullable, e.g.

SqlMapper.AddTypeHandler(typeof(LocalDate), LocalDateHandler.Default);
SqlMapper.AddTypeHandler(typeof(LocalDate?), LocalDateHandler.Default);

Parameterized query fails on Sybase AseConnection

The following code works with SqlConnection but fails with AseConnection. Both implement the IDbConnection interface so I assumed AseConnection would work. AseConnection works fine with queries that don't have parameters.

var data = connection.Query("select * from dbo.ORDER_HDR where ORD_NUM = @ORD_NUM", new { ORD_NUM = id });

The actual error is: AseException: COUNT field incorrect

Add GetTypeHandler(Type type) method to SqlMapper

To elegantly handle Oracle SYS_REFCURSOR out parameters, I am implementing a custom SqlMapper.IDynamicParameters class. In the AddParameters(IDbCommand command, SqlMapper.Identity identity) method I would like to use any registered ITypeHandler instances. The easiest way to acquire a reference to a registered instance would be via a GetTypeHandler(Type type) method on SqlMapper.

Don't override the DbType defined in ITypeHandler.SetValue.

While using the custom type handler ITypeHandler, the DbType of the parameter is defaulted to DbType.Object (SqlMapper.cs, L716).

This value overrides any changes made in the custom TypeHandler (SqlMapper.cs, L4026).

It would be great to be able to set the DbType in ITypeHandler.SetValue.

In my case, I would like to be able to save a specific object as a varchar (DbType.String) in the database (c.f. the example below).

The issue has been tested using MS SQLServer 2008.

Below is a sample of the code that produces the error.
Error message: System.Data.SqlClient.SqlException: Implicit conversion from data type sql_variant to varchar is not allowed. Use the CONVERT function to run this query.

public class Demo
{
    public static void Main(string[] args)
    {
        // Define the dynamic parameter.
        DynamicParameters dynamicParameters = new DynamicParameters();
        dynamicParameters.Add("@Bar", new Foo("Goo"));

        // Add the custom mapper.
        SqlMapper.AddTypeHandler<Foo>(new FooTypeHandler());

        // Execute the insert statement.
        SqlConnection conn = new SqlConnection(args[0] /* Connection string */);
        conn.Execute("INSERT INTO Foo (Bar) VALUES (@Bar)", dynamicParameters);
    }

    /// <summary>
    /// Empty class with one string property.
    /// </summary>
    class Foo
    {
        public string Bar { get; set; }

        public Foo(string value)
        {
            this.Bar = value;
        }
    }

    /// <summary>
    /// Very basic TypeHandler.
    /// </summary>
    class FooTypeHandler : SqlMapper.TypeHandler<Foo>
    {
        public override Foo Parse(object value)
        {
            return new Foo(value.ToString());
        }

        public override void SetValue(IDbDataParameter parameter, Foo value)
        {
            parameter.Value = value.Bar;

            // This value is overridden later on.
            parameter.DbType = DbType.String;
        }
    }
}

Dapper.Contrib Insert Fails

I am trying to use the Insert method from Dapper.Contrib and it keeps throwing an error.

using (SqlConnection conn = new SqlConnection(Program.ConnectionString))
{
    Author a = new Author
    {
        Username = "[email protected]",
        FullName = "James Hughes",
        CreatedDate = DateTime.Now
    };

    conn.Open();
    conn.Insert(a);
}

This code results in the following error

System.InvalidOperationException was unhandled
  Message=ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized.
  Source=System.Data
  StackTrace:
       at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction, String sql, Action`2 paramReader, Object obj, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Dapper\SqlMapper.cs:line 836
       at CallSite.Target(Closure , CallSite , Type , IDbConnection , IDbTransaction , String , Object , Object , Nullable`1 , Nullable`1 )
       at System.Dynamic.UpdateDelegates.UpdateAndExecute8[T0,T1,T2,T3,T4,T5,T6,T7,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
       at CallSite.Target(Closure , CallSite , Type , IDbConnection , IDbTransaction , String , Object , Object , Nullable`1 , Nullable`1 )
       at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Dapper\SqlMapper.cs:line 176
       at Dapper.Contrib.Extensions.SqlMapperExtensions.Insert[T](IDbConnection connection, T entityToInsert) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Dapper\Extensions\SqlMapperExtensions.cs:line 175
       at CodeSlice.Data.Profilers.ProfileInsert.<Run>b__2() in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Profilers\ProfileInsert.cs:line 51
       at CodeSlice.Data.Profilers.ActionProfiler.Profile(String description, Action actionToProfile) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Profilers\ActionProfiler.cs:line 16
       at CodeSlice.Data.Profilers.ProfileInsert.Run() in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Profilers\ProfileInsert.cs:line 39
       at CodeSlice.Data.Profilers.ProfileInsert.Exec() in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Profilers\ProfileInsert.cs:line 18
       at CodeSlice.Data.Program.Main(String[] args) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Program.cs:line 24
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Prevent multiexecute on parameters with type IDynamicMetaObjectProvider or IEnumerable<KeyValuePair<string, object>>

I use ExpandoObject for initialize Execute() method parameters. If take a look at SqlMapper.Execute() method, multiExec enabled for IEnumeable param, but it's unexpected behavior with ExpandoObject. Please, refine conditions like this:

if (multiExec != null && !(multiExec is string) &&
    // additional conditions:
    !(multiExec is IDynamicMetaObjectProvider) &&
    !(multiExec is IEnumerable<KeyValuePair<string, object>>))
{
    // execute as multiExec
}

Multi mapping query

Could you please increase count of input parameters up to 16, like it implemented by Microsoft BCL (for example Action<T1, ,T16> delegate)

Corrupt parameter replacements for lists, when one fieldname contains another

Hi guys,

I found an error for listed parameters:

First create a table like this:

CREATE TABLE DapperTest (
    Field INT PRIMARY KEY IDENTITY,
    Field_1 INT
);

Now use the following Query:

IEnumerable<dynamic> result = con.Query(
    "SELECT * FROM dbo.DapperTest WHERE Field IN @Field AND Field_1 IN @Field_1",
    new { Field = new[] { 1, 2 }, Field_1 = new[] { 2, 3 } });

You get the following exception:

{"Incorrect syntax near '_1'."}

Grabbed from SQL Profiler:

exec sp_executesql N'SELECT * FROM dbo.DapperTest WHERE Field IN (@Field1,@Field2) AND Field_1 IN (@Field1,@Field2)_1',N'@Field1 int,@Field2 int,@Field_11 int,@Field_12 int',@Field1=1,@Field2=2,@Field_11=2,@Field_12=3

As you can see Dapper renames all "@field" occurences regardless if the string ends there.

I had the same issue in my own SqlBuilder library and I replaced the exact string replacement with a replacement of the position and length given by System.Text.RegularExpressions.Match class. Maybe that helps.

Can't build solution without adding reference to System.Xml and F# 4.0.0

From a clean build, I have to add a reference to System.Xml to Dapper NET35, 40 and 45 for the solution to build.

To run DapperTests NET40 I also needed FSharp.Core 4.0.0 (I have 4.3, apparently) so I had to add a reference to the 4.0.0 nuget package.

Is this just me, or would this be useful to submit a PR for?

System.InvalidOperationException in FirebirdSql.Data.FirebirdClient

This code succeeds in raw ADO.net Executereader(), but fails in dapper Executereader().
IDataReader.Read() throws a System.InvalidOperationException exception with the message:

Invalid attempt of read when the reader is closed.
using System;
using System.Data;
using Dapper;
using FirebirdSql.Data.FirebirdClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var cs = @"initial catalog=localhost:database;user id=SYSDBA;password=masterkey";
            var sql = @"select count(*) from user";
            var con = new FbConnection(cs);
            con.Open();

            // raw ADO.net
            var sqlCmd = new FbCommand(sql, con);
            IDataReader reader1 = sqlCmd.ExecuteReader();
            while (reader1.Read())
            {
                Console.WriteLine(reader1[0].ToString()); // output is 10
            }

            // dapper
            var reader2 = con.ExecuteReader(sql);
            while (reader2.Read()) // System.InvalidOperationException. "Invalid attempt of read when the reader is closed."
            {
                Console.WriteLine(reader2[0].ToString()); 
            }
            Console.Read();
        }
    }
}

Stack trace:

System.InvalidOperationException was unhandled by user code.
  HResult=-2146233079
  Message=Invalid attempt of read when the reader is closed.
  Source=FirebirdSql.Data.FirebirdClient
  StackTrace:
       at FirebirdSql.Data.FirebirdClient.FbDataReader.CheckState()
       at FirebirdSql.Data.FirebirdClient.FbDataReader.Read()
       at ConsoleApplication2.Program.Main(String[] args) at c:\Users\aoki\Desktop\ConsoleApplication2\ConsoleApplication2\Program.cs:่กŒ 33
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Query<T> Invalid attempt to read from column ordinal

I've been using Dapper for about a month or so without any issues, but I ran into an issue today with the following code:

using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString))
{
    connection.Open();
    const string sql = @"select TOP(1) * from Order             
                            order by TimeOfOrderUtc desc";
    var order = connection.Query<Order>(sql).FirstOrDefault();
    return order;
}

This is returning the following:

System.Data.DataExceptionError parsing column 41 (TableNumber=Invalid attempt to read from column ordinal '41'. With CommandBehavior.SequentialAccess, you may only read from column ordinal '42' or greater.)

When using the multi-mapping APIs ... exception when stored procedure returns no rows from multiple recordsets

I have a stored procedure that takes in a number of parameters and returns two separate recordsets. It is called using this code

var queryObject = Connection.QueryMultiple("storedprocname", parameterObject,
commandType: CommandType.StoredProcedure);

        var resultObject1 = queryObject.Read<resultType1>();
        var resultObject2 = queryObject.Read<resultType2>();

If both recordsets contain 0 rows, the following exception occurs when attempting the first Read call above.

Message "When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id\r\nParameter name: splitOn"

Source "Dapper"

StackTrace " at Dapper.SqlMapper.GetTypeDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3264\r\n at Dapper.SqlMapper.GetDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1945\r\n at Dapper.SqlMapper.GridReader.ReadImpl[T](Type type, Boolean buffered) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3835\r\n at Dapper.SqlMapper.GridReader.Read[T](Boolean buffered) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3808\r\n . ....

TargetSite {System.Func`2[System.Data.IDataReader,System.Object] GetTypeDeserializer(System.Type, System.Data.IDataReader, Int32, Int32, Boolean)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

Running version 1.34 of Dapper, dated 11/09.2014 12:14.
.NET Framework v4.5
Provider = System.Data.SqlClient
SQL Server 11.0.5058

Getting System.InvalidCastException with new release

With this new release I am getting An exception when quering sql server database. The issue appears to be in IEnumerable QueryImpl(...) output conversion process. To my 1st look the parser block smells. The 1st and the last case do the same yet they are in different blocks. Perhaps the the 1st case was supposed to simply return val?
Current:
if (effectiveType == typeof(object))
{
yield return (T)Convert.ChangeType(val, effectiveType);
} else if (val == null || val is T) {
yield return (T)val;
} else {
yield return (T)Convert.ChangeType(val, effectiveType);
}
New:
if (effectiveType == typeof(object))
{
yield return val;
} else if (val == null || val is T) {
yield return (T)val;
} else {
yield return (T)Convert.ChangeType(val, effectiveType);
}
Error:
An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code
Additional information: Object must implement IConvertible.

Underlying exception is swallowed when reading SQL Server Types

When getting SQL Server Types (SqlGeography, SqlGeometry, SqlHierarchyId) from a query result, I was getting the following exception:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderInternalCompilerException was unhandled by user code
  HResult=-2146233088
  Message=An unexpected exception occurred while binding a dynamic operation
  Source=Microsoft.CSharp
  StackTrace:
       at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(DynamicMetaObjectBinder payload, IEnumerable`1 parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding)
       at Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(DynamicMetaObjectBinder action, RuntimeBinder binder, IEnumerable`1 args, IEnumerable`1 arginfos, DynamicMetaObject onBindingError)
       at Microsoft.CSharp.RuntimeBinder.CSharpConvertBinder.FallbackConvert(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
       at System.Dynamic.DynamicMetaObject.BindConvert(ConvertBinder binder)
       at System.Dynamic.ConvertBinder.Bind(DynamicMetaObject target, DynamicMetaObject[] args)
       at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
       at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
       at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
       at DATailor.Examples.Dapper.SqlClient.Test.AllTypesDAOTest.TestAllTypesDynamic()

Long story short, the following underlying exception was actually occurring and required that I add a bindingRedirect.

System.InvalidCastException was unhandled
  HResult=-2147467262
  Message=[A]Microsoft.SqlServer.Types.SqlGeometry cannot be cast to [B]Microsoft.SqlServer.Types.SqlGeometry. Type A originates from 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'. Type B originates from 'Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\11.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'.
  Source=DynamicGeometryIssue
  StackTrace:
       at DynamicGeometryIssue.TestDao.TestGeometry() in c:\Users\rich\Documents\Visual Studio 2013\Projects\DynamicGeometryIssue\DynamicGeometryIssue\TestDao.cs:line 27
       at DynamicGeometryIssue.Program.Main(String[] args) in c:\Users\rich\Documents\Visual Studio 2013\Projects\DynamicGeometryIssue\DynamicGeometryIssue\Program.cs:line 15
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

I'm not sure anything can be done about the exception being swallowed due to the use of dynamic, but if at all possible, it would save some people some headaches. I spent a good 4 hours trying to figure out what was going on here.

The issue is documented with more detail and formatted better on Stackoverflow http://stackoverflow.com/questions/25600043/runtimebinderinternalcompilerexception-when-using-microsoft-sqlserver-types-with

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.