dapper

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

让人想犯罪 __ 提交于 2019-11-27 10:19:55
问题 Yep, there are questions here and here about how to insert records with dapper-dot-net. However, the answers, while informative, didn't seem to point me in the right direction. Here is the situation: moving data from SqlServer to MySql. Reading the records into an IEnumerable<WTUser> is easy, but I am just not getting something on the insert. First, the 'moving records code': // moving data Dim session As New Session(DataProvider.MSSql, "server", _ "database") Dim resources As List(Of WTUser)

How to map to a Dictionary object from database results using Dapper Dot Net?

旧街凉风 提交于 2019-11-27 10:14:40
问题 If I have a simple query such as: string sql = "SELECT UniqueString, ID FROM Table"; and I want to map it to a dictionary object such as: Dictionary<string, int> myDictionary = new Dictionary<string, int>(); How would I do this with Dapper? I assume it is something like: myDictionary = conn.Query<string, int>(sql, new { }).ToDictionary(); But can't figure out the proper syntax. 回答1: There's various ways already shown; personally I'd just use the non-generic api: var dict = conn.Query(sql,

Dapper(一) 简介和性能

江枫思渺然 提交于 2019-11-27 10:02:35
Dapper的简介   Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的。Dapper只有一个代码文件,完全开源,你可以放在项目里的任何位置,来实现数据到对象的ORM操作,体积小速度快。 使用ORM的好处是增、删、改很快,不用自己写sql,因为这都是重复技术含量低的工作,还有就是程序中大量的从数据库中读数据然后创建model,并为model字段赋值。这些ORM都可以轻松给你搞定。ORM给我们开发带来便利时,性能也是一个让我们不得不考虑的问题。一般的ORM性能和直接写原生的sql比都差不少,但是Dapper性能还很错,甚至和DbHelperSQL方式性能高出很多。 Dapper的优势 Dapper是一个轻型的ORM类。代码就一个SqlMapper.cs文件,编译后体积小。 Dapper很快。Dapper的速度接近与IDataReader,取列表的数据超过了DataTable。 Dapper支持多数据库。诸如:Mysql,SqlLite,Mssql系列,Oracle等一系列的数据库。 Dapper的R支持多表并联的对象。支持一对多 多对多的关系。并且没侵入性,想用就用,不想用就不用,无XML无属性,代码以前怎么写现在还怎么写。 Dapper原理通过Emit反射IDataReader的序列队列

Dapper - a simple object mapper for .Net

别说谁变了你拦得住时间么 提交于 2019-11-27 10:02:27
Dapper - a simple object mapper for .Net Release Notes Located at stackexchange.github.io/Dapper Packages MyGet Pre-release feed: https://www.myget.org/gallery/dapper Package NuGet Stable NuGet Pre-release Downloads MyGet Dapper Dapper.Contrib Dapper.EntityFramework Dapper.EntityFramework.StrongName Dapper.Rainbow Dapper.SqlBuilder Dapper.StrongName Features Dapper is a NuGet library that you can add in to your project that will extend your IDbConnection interface. It provides 3 helpers: Execute a query and map the results to a strongly typed List public static IEnumerable<T> Query<T>(this

Dapper-小型ORM之王(C#.NET)

£可爱£侵袭症+ 提交于 2019-11-27 10:01:49
ORM: 对象关系映射器,它直接将数据库映射到C#对象。 有很多ORM框架可用,Dapper是其中之一,被称为ORM之王。 下面是Dapper主要的一些功能: 速度快,性能好; 更少的代码行 对象映射 静态对象绑定 动态对象绑定 易于处理Sql语句 易于处理存储过程 直接操作IDBConnection类,该类直接向数据库提供平滑性和运行查询,而不是像在EF和ADO.NET中那样使用各种对象传递数据。 多个查询支持 支持存储过程 批量处理数据插入 允许基于多个输入获取多个数据 为什么选择Dapper Dapper是第二快的ORM 图片参照: Dapper dotnet . 直接使用IDBConnection对象执行CRUD操作; 通过数据库提供查询静态和动态数据; 获取简单或复杂数据类型的通用结果; Dapper允许同时存储批量数据。 如何安装Dapper 在Visual Studio中,创建一个新的控制台项目,并在解决方案资源管理器中右键单击引用,选择 “管理Nuget包...”包管理器,然后搜索Dapper,并使用NuGet包管理器控制台命令“install-package Dapper”,这将在项目中安装Dapper。 Dapper如何工作 主要包含三个步骤 第一步:使用连接字符串创建一个IDBConnection对象; 第二步:编写一个查询并将其存储在一个普通的字符串变量中;

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

自闭症网瘾萝莉.ら 提交于 2019-11-27 09:23:56
Compile Error 'System.Data.SqlClient.SqlConnection' has no applicable method named 'Query' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. Now, I know how to work around the problem, but I'm trying to get a better understanding of the error itself. I have class that I'm building to leverage Dapper. In the end I'm going to provide some more custom functionality to make our type of data access a lot more streamlined. In particular

Closing connection when using Dapper

ぃ、小莉子 提交于 2019-11-27 08:45:01
Is it necessary to close connection once query is executed explicitly calling Close method or putting the connection within Using statement? Would leaving connection open lead to connection reuse and improve SQL performance for future queries? 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 (if it was

Dapper.Rainbow VS Dapper.Contrib

 ̄綄美尐妖づ 提交于 2019-11-27 06:13:48
Can someone please explain the difference between Dapper.Rainbow vs. Dapper.Contrib ? I mean when do you use SqlMapperExtensions.cs of Dapper.Contrib and when should you use Dapper.Rainbow? Metro Smurf I’ve been using Dapper for a while now and have wondered what the Contrib and Rainbow projects were all about myself. After a bit of code review, here are my thoughts on their uses: Dapper.Contrib Contrib provides a set of extension methods on the IDbConnection interface for basic CRUD operations: Get Insert Update Delete The key component of Contrib is that it provides tracking for your

Why doesn't Dapper dot net open and close the connection itself?

谁说我不能喝 提交于 2019-11-27 04:47:13
Dapper implicitly expects a connection to be open when it uses it. Why doesn't it open and close it itself? Wouldn't this simply connection management? I ask because a co-worker and I have been going back and forth on the nature of what goes on behind the scenes with connection pooling, and if there is any benefit to keeping a connection open amongst multiple commands, or to open and close it for each command. Dapper now (and for quite some time) deals with this internally. It just works™ Original (outdated) answer: You aren't wrong. The reason I hadn't noticed this inconvenience is that for

Using Dapper to map more than 5 types

不问归期 提交于 2019-11-27 04:20:35
问题 I am currently building a SELECT query that joins 12 tables together. I've been using Dapper for all my other queries and it works great. Problem is, the generic methods only have to five generic parameters. I've previously modified the code to support up to 6 for another query, but now I really don't think I should be hacking 6 more levels of generics. Is there a way to pass dapper an array of types, and it returns the results as an array of objects, which I can cast manually if I have to? I