public static void SelectDapper() { using (TestEntities context = new TestEntities()) { watch.Restart(); var result = context.Database.Connection.Query <User>("select top 2 * from [user] where id > 1 order by id"); Console.WriteLine("Select - {0}ms", watch.ElapsedMilliseconds); } Console.WriteLine(); }
public static void ToDapper() { using (TestEntities context = new TestEntities()) { object result = null; watch.Restart(); result = (from o in context.Users where o.Id > 1 orderby o.Id select o).ToDapper().ToList(); Console.WriteLine("ToDapper - {0}ms", watch.ElapsedMilliseconds); } }
public static void InsertEF() { using (TestEntities context = new TestEntities()) { watch.Restart(); context.Users.Add(new User() { Name = "User Insert", DateCreate = DateTime.Now, Gender = Gender.Female }); context.SaveChanges(); Console.WriteLine("Insert - {0}ms", watch.ElapsedMilliseconds); } Console.WriteLine(); }
public static void DeleteDapper() { using (TestEntities context = new TestEntities()) { watch.Restart(); context.Database.Connection.Execute("delete from [BlogPost]"); Console.WriteLine("Delete All - {0}ms", watch.ElapsedMilliseconds); Console.WriteLine(); watch.Restart(); context.Database.Connection.Execute("delete from [BlogPost] where id = 6"); Console.WriteLine("Delete With Query - {0}ms", watch.ElapsedMilliseconds); } Console.WriteLine(); }
public static void Delete() { using (TestEntities context = new TestEntities()) { watch.Restart(); context.BlogPosts.Delete(); Console.WriteLine("Delete All - {0}ms", watch.ElapsedMilliseconds); Console.WriteLine(); watch.Restart(); context.Users.Delete(o => o.Id == 6); Console.WriteLine("Delete With Query - {0}ms", watch.ElapsedMilliseconds); } Console.WriteLine(); }
public User GetUser(int id) { using (TestEntities dbContext = new TestEntities()) { var user = new User(); try { user = dbContext.Users.First(p => p.Id == id); } catch (Exception) { // write to file logout.log } return(user); } }
static void Main(string[] args) { watch.Restart(); using (TestEntities context = new TestEntities()) { //context.Optimization(); context.Initialize(); } Console.WriteLine("{0}ms - Initialize Context", watch.ElapsedMilliseconds); TestsDapper(); Tests(); TestsEF(); Console.ReadLine(); }
public static void Select() { using (TestEntities context = new TestEntities()) { watch.Restart(); var result = context.Users.Query(o => o.Id > 1, 2, orderBy => orderBy.Asc(o => o.Id).Desc(o => o.Name)); int qtd = result.Count(); Console.WriteLine("Select - {0}ms", watch.ElapsedMilliseconds); watch.Restart(); var result2 = context.Users.Query(o => new { o.Id, o.Name }, o => o.Id > 1, 2, orderBy => orderBy.Asc(o => o.Id).Desc(o => o.Name)); qtd = result2.Count(); Console.WriteLine("Select With Selector - {0}ms", watch.ElapsedMilliseconds); } Console.WriteLine(); }
public static void InsertDapper() { using (TestEntities context = new TestEntities()) { var obj = new { Name = "User Insert", DateCreate = DateTime.Now, Gender = Gender.Female }; watch.Restart(); context.Database.Connection.Query <Int64>("insert into [user] (name, datecreate, gender) values (@Name, @DateCreate, @Gender); SELECT CAST(SCOPE_IDENTITY() as bigint)", obj).FirstOrDefault(); Console.WriteLine("Insert - {0}ms", watch.ElapsedMilliseconds); } Console.WriteLine(); }
public int SaveUser(User user) { using (TestEntities dbContext = new TestEntities()) { var ExistUser = new User(); ExistUser = GetUser(user.Login); if (ExistUser.Id == 0) { dbContext.Users.Add(user); dbContext.SaveChanges(); return(0); } else { return(-1); } } }
public static void UpdateEF() { using (TestEntities context = new TestEntities()) { User user = new User { Gender = Gender.Male, Id = 1, Name = "teste", DateCreate = DateTime.Now }; watch.Restart(); context.Entry(user).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); Console.WriteLine("Update All - {0}ms", watch.ElapsedMilliseconds); } Console.WriteLine(); }
public static void Insert() { using (TestEntities context = new TestEntities()) { var obj = new { Name = "User Insert", DateCreate = DateTime.Now, Gender = Gender.Female }; watch.Restart(); context.Users.Insert(obj); Console.WriteLine("Insert With Anonymous - {0}ms", watch.ElapsedMilliseconds); Console.WriteLine(); watch.Restart(); var obj2 = new User() { Name = "User Insert", DateCreate = DateTime.Now, Gender = Gender.Male }; context.Users.Insert(obj2); Console.WriteLine("Insert With Class - {0}ms", watch.ElapsedMilliseconds); Console.WriteLine(); watch.Restart(); context.Users.Insert(obj, returnIdentity: false); Console.WriteLine("Insert Without Return Identity - {0}ms", watch.ElapsedMilliseconds); Console.WriteLine(); watch.Restart(); context.Users.Insert(obj, propertyKey: "User_Id"); Console.WriteLine("Insert With Change PropertyKey - {0}ms", watch.ElapsedMilliseconds); } Console.WriteLine(); }