Created
January 16, 2012 23:10
-
-
Save SamSaffron/1623514 to your computer and use it in GitHub Desktop.
Dapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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