Created
November 13, 2017 16:42
-
-
Save anonymous/dcd4f329dc9dbb4f3f5b05ce00fe1a9b to your computer and use it in GitHub Desktop.
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
using Microsoft.EntityFrameworkCore; | |
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace IMastersQueryCompiled | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Carregar instancia (pra não ser injusto) | |
using (var db = new IMastersContext()) | |
{ | |
//Recriar banco de dados | |
db.Database.EnsureDeleted(); | |
db.Database.EnsureCreated(); | |
//Popular Tabela | |
for (int i = 0; i < 500; i++) | |
{ | |
db.Pessoas.Add(new Pessoa | |
{ | |
Nome = $"IMasters Sample {(i + 1)}", | |
Senha = $"IMASTERS{(i + 1):00000}" | |
}); | |
} | |
db.SaveChanges(); | |
var pessoa = db.Pessoas.First(); | |
} | |
ExecutarTeste( | |
senhas => | |
{ | |
using (var context = new IMastersContext()) | |
{ | |
foreach (var id in senhas) | |
{ | |
var pessoa = context.Pessoas.Single(c => c.Senha == id); | |
} | |
} | |
}, | |
nomeTeste: "Query Normal"); | |
ExecutarTeste( | |
senhas => | |
{ | |
// Exemplo de como criar a query compilada | |
var query = EF.CompileQuery((IMastersContext db, string id) | |
=> db.Pessoas.Single(c => c.Senha == id)); | |
using (var context = new IMastersContext()) | |
{ | |
foreach (var id in senhas) | |
{ | |
query(context, id); | |
} | |
} | |
}, | |
nomeTeste: "Query Compilada"); | |
Console.ReadKey(); | |
} | |
private static void ExecutarTeste(Action<string[]> teste, string nomeTeste) | |
{ | |
var senhas = GetSenhas(500); | |
var tempo = new Stopwatch(); | |
tempo.Start(); | |
teste(senhas); | |
tempo.Stop(); | |
Console.WriteLine($"{nomeTeste.PadRight(15, ' ')}: {tempo.ElapsedMilliseconds.ToString().PadLeft(4)}ms"); | |
} | |
private static string[] GetSenhas(int quantidade) | |
{ | |
var senhas = new string[quantidade]; | |
for (var i = 0; i < quantidade; i++) | |
{ | |
senhas[i] = $"IMASTERS{(i + 1):00000}"; | |
} | |
return senhas; | |
} | |
} | |
public class IMastersContext : DbContext | |
{ | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
optionsBuilder.UseSqlServer( | |
"data source=.\\Sql2016;initial catalog=IMasters;integrated security=True;MultipleActiveResultSets=True;ConnectRetryCount=0"); | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
modelBuilder.Entity<Pessoa>( | |
entity => | |
{ | |
entity.ToTable("Pessoa"); | |
entity.Property(e => e.DataModificacao) | |
.HasColumnType("datetime") | |
.HasDefaultValueSql("getdate()"); | |
entity.Property(e => e.Identificacao).HasDefaultValueSql("newid()"); | |
}); | |
} | |
public virtual DbSet<Pessoa> Pessoas { get; set; } | |
} | |
public class Pessoa | |
{ | |
public int PessoaID { get; set; } | |
public string Nome { get; set; } | |
public string Senha { get; set; } | |
public Guid Identificacao { get; set; } | |
public DateTime DataModificacao { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment