Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dapper
// inserting 500 products.
// even when Dapper is running the same sql as EF it is twice as fast.
var products = Enumerable.Range(1, 500).Select(_ =>
new Product
{
Name = Guid.NewGuid().ToString(),
Description = Guid.NewGuid().ToString()
}
).ToList();
Stopwatch sw = Stopwatch.StartNew();
using (var ctx = new MyContext(cnn))
{
foreach (var p in products) ctx.Products.Add(p);
ctx.SaveChanges();
}
Console.WriteLine(sw.ElapsedMilliseconds);
// 2023
sw.Restart();
cnn.Open();
foreach (var p in products)
{
cnn.Query<int>(@"insert [dbo].[Products]([Name], [Description])
values (@Name, @Description)
select [Id]
from [dbo].[Products]
where @@ROWCOUNT > 0 and [Id] = scope_identity()", p);
}
Console.WriteLine(sw.ElapsedMilliseconds);
// 951
sw.Restart();
cnn.Execute("insert Products(Name,Description) values(@Name,@Description)", products.Select(p => new { p.Name, p.Description }));
Console.WriteLine(sw.ElapsedMilliseconds);
// 777
Console.ReadKey();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.