Tag: dapper

有没有办法用Dapper调用存储过程?

Dapper Micro ORM的结果给我留下了非常深刻的印象。 我正在考虑为我的新项目,但我有一个担心,有些时候我的项目需要有存储过程,我有很多在网上search,但没有发现任何存储过程。 那么有没有什么办法让Dapper能够使用存储过程呢? 请让我知道如果有可能,否则我必须按照我的方式扩展它。

我如何执行插入并返回与Dapper插入的身份?

如何执行插入到数据库并返回插入的身份与Dapper? 我试过这样的事情: string sql = "DECLARE @ID int; " + "INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff); " + "SELECT @ID = SCOPE_IDENTITY()"; var id = connection.Query<int>(sql, new { Stuff = mystuff}).First(); 但它没有工作。 @Marc Gravell谢谢,回复。 我试过你的解决scheme,但是,仍然是exception跟踪在下面 System.InvalidCastException: Specified cast is not valid at Dapper.SqlMapper.<QueryInternal>d__a`1.MoveNext() in (snip)\Dapper\SqlMapper.cs:line 610 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, […]

如何在Dapper.Net中编写一对多的查询?

我写了这个代码来投影一对多的关系,但它不工作: using (var connection = new SqlConnection(connectionString)) { connection.Open(); IEnumerable<Store> stores = connection.Query<Store, IEnumerable<Employee>, Store> (@"Select Stores.Id as StoreId, Stores.Name, Employees.Id as EmployeeId, Employees.FirstName, Employees.LastName, Employees.StoreId from Store Stores INNER JOIN Employee Employees ON Stores.Id = Employees.StoreId", (a, s) => { a.Employees = s; return a; }, splitOn: "EmployeeId"); foreach (var store in stores) { […]

用scope_identity()用法执行嵌套大容量插入的最快方法?

在我们的一个(C#)应用程序中,我们插入/更新一个大图(100个插入和更新)。 这是包装在一个事务中,因为我需要整个事情回滚的情况下出现错误。 我们正在使用Dapper来执行SQL语句。 不幸的是,整个手术目前需要2到8秒。 这是2到8秒我们的数据库中的核心表被locking,导致其他应用程序挂起或遭受锁。 我确定其中的一个操作插入到一个包含超过1亿2千万条logging的表中,大部分时间都占用了,但是我不确定如何优化这个操作。 粗略地说,该表格图如下设置: table A ( id int primary_key, name nvarchar ) table B ( id int primary_key, a_id int foreign_key, # has an index name nvarchar ) 将数据插入A ,还需要将相应的数据插入到B 。 因此,我使用scope_identity()来获取logging的ID并在将logging插入到B时使用它。 伪明智的看起来如下: # open transaction # perform other table inserts # # this is one of the slowest operations for […]