private static void TestarDapper(EventoContext ctx) { var tempo = new Stopwatch(); CriarBanco(ctx); var dapper = (SqlConnection)ctx.Database.GetDbConnection(); if (dapper.State != System.Data.ConnectionState.Open) { dapper.Open(); } foreach (var item in _opcoesInsercao) { var eventos = GetEventos(item, item); tempo.Restart(); dapper.Insert(eventos); //dapper.BulkInsert(eventos); tempo.Stop(); Console.WriteLine($"Tempo Insert Dapper {item.ToString().PadLeft(5, ' ')} Registro(s): {tempo.Elapsed}"); } foreach (var item in _opcoesInsercao) { tempo.Restart(); var eventos = dapper.Query <Evento>($"select top {item} * from Eventos").ToList(); tempo.Stop(); Console.WriteLine($"Tempo Select Dapper {item.ToString().PadLeft(5, ' ')} Registro(s): {tempo.Elapsed}"); } }
private static void TestarEFCore(EventoContext ctx) { var tempo = new Stopwatch(); CriarBanco(ctx); // ctx.ChangeTracker.AutoDetectChangesEnabled = false; foreach (var item in _opcoesInsercao) { var eventos = GetEventos(item, item).ToList(); tempo.Restart(); ctx.AddRange(eventos); ctx.SaveChanges(); tempo.Stop(); Console.WriteLine($"Tempo Insert EF Core {item.ToString().PadLeft(5, ' ')} Registro(s): {tempo.Elapsed}"); } ctx.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; foreach (var item in _opcoesInsercao) { tempo.Restart(); var eventos = ctx.Set <Evento>().Take(item).ToList(); tempo.Stop(); Console.WriteLine($"Tempo Select EF Core {item.ToString().PadLeft(5, ' ')} Registro(s): {tempo.Elapsed}"); } }
private static void CriarBanco(EventoContext ctx) { if (!ctx.Database.EnsureCreated()) { ctx.Database.ExecuteSqlCommand("DELETE FROM Eventos"); ctx.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Eventos', RESEED, 0)"); } }
static void Main(string[] args) { for (int i = 1; i <= _for; i++) { Executar( Dapper: () => { using (var dapper = new EventoContext()) { TestarDapper(dapper); } }, EFCore: () => { using (var efcore = new EventoContext()) { TestarEFCore(efcore); } }); } Console.ReadKey(); }