dapper

sp_executesql is slow with parameters

落花浮王杯 提交于 2019-11-27 01:53:54
问题 I'm using dapper-dot-net as an ORM and it produces the following, slow-executing (1700ms), SQL code. exec sp_executesql N'SELECT TOP 5 SensorValue FROM "Values" WHERE DeviceId IN (@id1,@id2) AND SensorId = @sensor AND SensorValue != -32768 AND SensorValue != -32767',N'@id1 bigint,@id2 bigint,@sensor int',@id1=139,@id2=726,@sensor=178 When I modify this code by removing the parameters the query executes blazingly fast (20ms). Should the lack of these parameters actually make this big

Explanation of dapper buffer/cache

元气小坏坏 提交于 2019-11-27 00:35:00
问题 I use dapper to return objects from my database as IEnumerable. As default dapper has buffer setting set to true. How does this work? If dapper cache the first query and then get the objects from memory. What happens if someone edit/delete/add rows in the table. Must dapper recache all data again for this query? 回答1: The buffer is unrelated to cache. Dapper does not include any kind of data-cache (although it does have a cache related to how it processes commands, i.e. "this command string,

How to insert a C# List to database using Dapper.NET

两盒软妹~` 提交于 2019-11-27 00:33:05
问题 Using dapper, how can I insert a C# List to database. Previously without dapper I used the below code to insert the List values to database . try { connection.Open(); for (int i = 0; i < processList.Count; i++) { string processQuery = "INSERT INTO PROCESS_LOGS VALUES (@Id, @st_Time, @ed_Time, @td_Time)"; command = new SqlCommand(processQuery, connection); command.Parameters.Add("Id", SqlDbType.Int).Value = processList[i].ID; command.Parameters.Add("st_Time", SqlDbType.DateTime).Value =

How to use transactions with dapper.net?

橙三吉。 提交于 2019-11-27 00:04:57
问题 I would like to run multiple insert statements on multiple tables. I am using dapper.net. I don't see any way to handle transactions with dapper.net. Please share your ideas on how to use transactions with dapper.net. 回答1: Here the code snippet: using System.Transactions; .... using (var transactionScope = new TransactionScope()) { DoYourDapperWork(); transactionScope.Complete(); } Note that you need to add reference to System.Transactions assembly because it is not referenced by default. 回答2

How do I perform an insert and return inserted identity with Dapper?

≯℡__Kan透↙ 提交于 2019-11-26 23:36:58
How do I perform an insert to database and return inserted identity with Dapper? I've tried something like this: 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(); But it did't work. @Marc Gravell thanks, for reply. I've tried your solution but, still same exception trace is below 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

How do I select an aggregate object efficiently using Dapper?

左心房为你撑大大i 提交于 2019-11-26 22:35:21
问题 Lets say that I have a series of objects that form an aggregate. public class C{ public string Details {get;set;} } public class B{ public string Details {get;set;} public List<C> Items {get;set;} } public class A{ public long ID {get;set;} public string Details {get;set;} public List<B> Items {get;set;} } using Dapper, what is the best way to populate these from tables in a database (in my case it's postgres but that shouldn't matter). The tables in the example are pretty much one for one

Bulk inserts taking longer than expected using Dapper

人盡茶涼 提交于 2019-11-26 21:41:49
After reading this article I decided to take a closer look at the way I was using Dapper. I ran this code on an empty database var members = new List<Member>(); for (int i = 0; i < 50000; i++) { members.Add(new Member() { Username = i.toString(), IsActive = true }); } using (var scope = new TransactionScope()) { connection.Execute(@" insert Member(Username, IsActive) values(@Username, @IsActive)", members); scope.Complete(); } it took about 20 seconds. That's 2500 inserts/second. Not bad, but not great either considering the blog was achieving 45k inserts/second. Is there a more efficient way

Why Entity Framework performs faster than Dapper in direct select statement [closed]

流过昼夜 提交于 2019-11-26 18:25:23
问题 I'm new to using ORM in dealing with database, Currently I'm making a new project and I have to decide if i'll use Entity Framework or Dapper. I read many articles which says that Dapper is faster than Entity Framework. So I made 2 simple prototype projects one using Dapper and the other uses Entity Framework with one function to get all the rows from one table. The table schema as the following picture and the code for both projects as the following for Dapper project System.Diagnostics

Performing Inserts and Updates with Dapper

怎甘沉沦 提交于 2019-11-26 18:12:36
I am interested in using Dapper - but from what I can tell it only supports Query and Execute. I do not see that Dapper includes a way of Inserting and Updating objects. Given that our project (most projects?) need to do inserts and updates, what is the best practice for doing Inserts and Updates alongside dapper? Preferably we would not have to resort to the ADO.NET method of parameter building, etc. The best answer I can come up with at this point is to use LinqToSQL for inserts and updates. Is there a better answer? Sam Saffron We are looking at building a few helpers, still deciding on

Is there a way to call a stored procedure with Dapper?

左心房为你撑大大i 提交于 2019-11-26 18:06:58
I am very impressed with the results of Dapper Micro ORM for stackoverflow.com. I am considering it for my new project and but I have one concern about that some times my project requires to have Stored Procedure and I have search a lot on web but not found anything with stored procedure. So is there any way to have Dapper work with a stored procedure? Please let me know if it is possible otherwise I have to extend it in my way. Sam Saffron In the simple case you can do: var user = cnn.Query<User>("spGetUser", new {Id = 1}, commandType: CommandType.StoredProcedure).First(); If you want