private void button3_Click(object sender, EventArgs e) { //在上面的方法中 会调用自动检测功能。 这个功能默认是开启的 当我们在做批量操作时 可以关闭这个来提高性能 .例如 //List<tb> tbs = new List<tb>(); //using (var context = new PermissionEntities()) //{ // try // { // context.Configuration.AutoDetectChangesEnabled = false; // foreach (var unicorn in tbs) // { // context.tb.Add(unicorn); // } // // context.tb.AddRange(tbs); // } // finally // { // context.Configuration.AutoDetectChangesEnabled = true; // } //} using (var db = new PermissionEntities()) { string name = dataGridView1.CurrentRow.Cells[0].Value.ToString(); var tbs = db.tb.Where(t => t.name == name).FirstOrDefault(); db.tb.Remove(tbs); //db.Entry(tbs).State = EntityState.Deleted; db.SaveChanges(); } }
private void button2_Click(object sender, EventArgs e) { Stopwatch watch = new Stopwatch(); watch.Reset(); watch.Start(); //只有.net 4.5 才行 var options = new BulkInsertOptions { EnableStreaming = true, }; List <tb> entities = new List <tb>(); for (int i = 0; i < 50000; i++) { tb entity = new tb(); entity.name = "60000" + i; entity.password = "******" + entity.name; entities.Add(entity); } using (var db = new PermissionEntities()) { db.BulkInsert(entities, options); } watch.Stop(); MessageBox.Show("插入成功:耗时:" + watch.ElapsedMilliseconds); }
private void button1_Click(object sender, EventArgs e) { Stopwatch watch = new Stopwatch(); watch.Reset(); watch.Start(); using (var db = new PermissionEntities()) { //看看EF EL怎么解决 // 复用的查询 // base query var q = db.tb.Where(t => true); // get total count var q1 = q.FutureCount(); // get page var q2 = q.OrderBy(t => t.name).Skip((int.Parse(textBox1.Text.Trim()) - 1) * 1000).Take(1000).Future(); // triggers execute as a batch int total = q1.Value; var tasks = q2.ToList(); label1.Text = total.ToString(); dataGridView1.DataSource = tasks; } watch.Stop(); MessageBox.Show("EntityFramework.Extension耗时:" + watch.ElapsedMilliseconds); }
private void button1_Click(object sender, EventArgs e) { Stopwatch watch = new Stopwatch(); watch.Reset(); watch.Start(); List <tb> entities = new List <tb>(); using (var db = new PermissionEntities()) { using (var transactionScope = new TransactionScope()) { // some stuff in dbcontext for (int i = 0; i < 20000; i++) { tb entity = new tb(); entity.name = "40000" + i; entity.password = "******" + entity.name; entities.Add(entity); } db.BulkInsert(entities); //db.SaveChanges(); transactionScope.Complete(); } } watch.Stop(); MessageBox.Show("插入成功:耗时:" + watch.ElapsedMilliseconds); }
private void button2_Click(object sender, EventArgs e) { using (var db = new PermissionEntities()) { db.tb.Where(u => u.name.Contains("4")).Delete(); } }
private void button2_Click(object sender, EventArgs e) { using (var db = new PermissionEntities()) { var tbs = db.tb.Where(t => t.name == "100").FirstOrDefault(); tbs.password = "******"; db.SaveChanges(); } }
private void button2_Click(object sender, EventArgs e) { using (var enty = new PermissionEntities()) { var lambda = lambdaBuilder1.BuildLambda <Bse_User>(); var users = enty.Bse_User.AsQueryable().Where(lambda).ToList(); dataGridView1.DataSource = users; } }
private List <tb> DoQuery() { using (var db = new PermissionEntities()) { var tbs = db.tb.Where(p => p.name.Contains("1")).OrderBy(p => p.name); List <tb> lstTb = tbs.ToList(); return(lstTb); } }
private void FrmLambdaBuilderTest_Load(object sender, EventArgs e) { using (var enty = new PermissionEntities()) { var users = enty.Bse_User.ToList(); dataGridView1.DataSource = users; } lambdaBuilder1.Init <Bse_User>(dataGridView1); }
private void button6_Click(object sender, EventArgs e) { //原生支持 using (var db = new PermissionEntities()) { //db.tb.SqlQuery //db.Database.SqlQuery var tbs = db.Database.ExecuteSqlCommand("update tb set password='******' where name='100'"); } }
private List <tb> DoQuery2() { using (var db = new PermissionEntities()) { var tbs = db.Database.SqlQuery <tb>("select * from tb"); List <tb> lstTb = tbs.ToList(); return(lstTb); } }
private List <tb> DoQuery1() { using (var db = new PermissionEntities()) { var tbs = from tbContext in db.tb where tbContext.name.Contains("2") orderby tbContext.name select tbContext; List <tb> lstTb = tbs.ToList(); return(lstTb); } }
private void button4_Click(object sender, EventArgs e) { using (var db = new PermissionEntities()) { db.tb.Update( u => u.name.Contains("2"), u2 => new tb { password = "******" }); //或者 db.tb.Where(u => u.name == "2").Update(u => new tb { password = "******" }); } }
private void button1_Click(object sender, EventArgs e) { string name = dataGridView1.CurrentRow.Cells[0].Value.ToString(); using (var db = new PermissionEntities()) { db.tb.Where(u => u.name == name).Delete(); //或者 // db.tb.Delete(u => u.name == "101"); //不需要db.SaveChanges(); } }
private void button8_Click(object sender, EventArgs e) { Stopwatch watch = new Stopwatch(); watch.Reset(); watch.Start(); using (var db = new PermissionEntities()) { db.tb.Where(t => t.name.Contains("2")).OrderBy(t => t.name).AsNoTracking().ToList(); } watch.Stop(); MessageBox.Show("耗时:" + watch.ElapsedMilliseconds); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); using (var dbcontext = new PermissionEntities()) { var objectContext = ((IObjectContextAdapter)dbcontext).ObjectContext; var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); mappingCollection.GenerateViews(new List <EdmSchemaError>()); } Application.Run(new FrmMainBase()); }
private void button7_Click(object sender, EventArgs e) { //EF一定要有主键,否则报错 using (var enty = new PermissionEntities()) { tb aa = new tb(); aa.name = "10000"; aa.password = "******" + aa.name; enty.tb.Add(aa); enty.tb.Attach(aa); enty.SaveChanges(); } }
private void button5_Click(object sender, EventArgs e) { Stopwatch watch = new Stopwatch(); watch.Reset(); watch.Start(); using (var db = new PermissionEntities()) { var query = db.tb.Where(t => true).OrderBy(t => t.name); //用skip之前必须OrderBy PagerResultModel <tb> tbs = PagerExtension.ToPager <tb>(query, 1000, int.Parse(textBox1.Text.Trim())); label1.Text = tbs.Count.ToString(); dataGridView1.DataSource = tbs.List; } watch.Stop(); MessageBox.Show("ExfSoft.EntityFramework.Extension耗时:" + watch.ElapsedMilliseconds); }
private void button1_Click(object sender, EventArgs e) { //EF一定要有主键,否则报错 using (var enty = new PermissionEntities()) { for (int i = 0; i < 200; i++) { tb aa = new tb(); aa.name = (200 + i).ToString(); aa.password = "******" + aa.name; enty.tb.Add(aa); } enty.SaveChanges(); } MessageBox.Show("成功"); }
private void button5_Click(object sender, EventArgs e) { using (var db = new PermissionEntities()) { //看看EF EL怎么解决 // 复用的查询 // base query var q = db.tb.Where(t => t.name.Contains("1")); // get total count var q1 = q.FutureCount(); // get page var q2 = q.OrderBy(t => t.name).Skip((int.Parse(textBox1.Text.Trim()) - 1) * 10).Take(10).Future(); // triggers execute as a batch int total = q1.Value; var tasks = q2.ToList(); label1.Text = total.ToString(); dataGridView1.DataSource = tasks; } }
private void button5_Click(object sender, EventArgs e) { Stopwatch watch = new Stopwatch(); watch.Reset(); watch.Start(); List <tb> tbs = new List <tb>(); for (int i = 0; i < 10000; i++) { tb entity = new tb(); entity.name = "10000" + i; entity.password = "******" + entity.name; tbs.Add(entity); } using (var db = new PermissionEntities()) { db.BulkInsert(tbs); } watch.Stop(); MessageBox.Show("插入成功:耗时:" + watch.ElapsedMilliseconds); }