Using Dapper to populate Enum properties

Sure – as long as your enum agrees, i.e. enum MyEnumType : byte { Foo, Bar, Blip, … } then it will all work automatically. (this limitation is by design, and shared with LINQ-to-SQL as it happens) Alternatively, if the enum is : int and can’t be changed, cast it in the SQL: SELECT …, … Read more

Closing connection when using Dapper

I am assuming that you are using latest version of Dapper. With Dapper, there are two ways to manage connection: Fully manage yourself: Here, you are fully responsible for opening and closing connection. This is just like how you treat connection while working with ADO.NET. Allow Dapper to manage it: Dapper automatically opens the connection … Read more

How can I map .NET DateTime to database DateTime2 using Dapper to avoid “SqlDateTime overflow” exception?

There’s a much easier solution now in a similar question, but it is about string. For DateTime: SqlMapper.AddTypeMap(typeof(DateTime), System.Data.DbType.DateTime2); This must be applied before any database call like INSERT. You can put it in your Main or Startup class, or any place that runs on Startup to configure data access. SqlMapper is a static class … Read more

Dapper: Result from “SELECT COUNT(*) FROM TableName

Please don’t do this! It’s fragile, and introduces a gaping sql injection vulnerability. If you can return your count for a given table with one line of very expressive code, and no vulnerability, why make it method? Do this instead: DapperConnection.ExecuteScalar<int>(“SELECT COUNT(*) FROM customers”); // You will be happier and live longer if you avoid … Read more

Dapper AddDynamicParams for IN statement with “dynamic” parameter name

I have just submitted a fix to the repository that allows any of the following to work correctly: by object (this worked previously): values.AddDynamicParams(new { ids = list }); or, by single name: values.Add(“ids”, list); or, as a dictionary: var args = new Dictionary<string, object>(); args.Add(“ids”, list); values.AddDynamicParams(args); I have not yet deployed to NuGet. … Read more

Inserting an IEnumerable collection with Dapper errors out with “class is not supported by Dapper.”

I just added a test for this: class Student { public string Name {get; set;} public int Age { get; set; } } public void TestExecuteMultipleCommandStrongType() { connection.Execute(“create table #t(Name nvarchar(max), Age int)”); int tally = connection.Execute(@”insert #t (Name,Age) values(@Name, @Age)”, new List<Student> { new Student{Age = 1, Name = “sam”}, new Student{Age = 2, … Read more

What causes “extension methods cannot be dynamically dispatched” here?

So, can somebody please help me understand why leveraging the same overload inside of those other methods is failing with that error? Precisely because you’re using a dynamic value (param) as one of the arguments. That means it will use dynamic dispatch… but dynamic dispatch isn’t supported for extension methods. The solution is simple though: … Read more