EntityFramework 4.3 数据操作
using (TestEntities te = new TestEntities()) { //增加数据 //Table01 t01 = new Table01(); //t01.Name = "Name01"; //t01.Age = 21; //t01.NDate1 = DateTime.Now; //te.Table01.AddObject(t01); //te.SaveChanges(); //增加数据完成 //修改数据 //Table01 t01 = te.Table01.Where(t => t.ID == 1).SingleOrDefault<Table01>(); //t01.Name = "Name02"; //te.SaveChanges(); //修改数据完成 //删除数据 //Table01 t01 = te.Table01.Where(t => t.ID == 1).SingleOrDefault<Table01>(); //te.DeleteObject(t01); //te.SaveChanges(); //删除数据完成 //查询单表 //var result = te.Table01.Where(t => t.ID == 1).SingleOrDefault<Table01>(); // 或 //var result = (from t in te.Table01 // where t.ID == 1 // select t).SingleOrDefault<Table01>(); //查询单表完成 //多表联合查询 //var result = (from t01 in te.Table01 // join t02 in te.Table02 // on t01.ID equals t02.Table01ID // orderby t01.ID ascending // select new { t01, t02 }).ToList(); //多表联合查询完成 //多表联合查询-左关联 //var result = (from t01 in te.Table01 // join t02 in te.Table02 // on t01.ID equals t02.Table01ID // into xing // from t02 in xing.DefaultIfEmpty() // orderby t01.ID descending // select new { t01, t02 }).ToList(); //多表联合查询完成-左关联 //多表联合查询-右关联 //var result = (from t02 in te.Table02 // join t01 in te.Table01 // on t02.Table01ID equals t01.ID // into xing // from t01 in xing.DefaultIfEmpty() // orderby t01.ID ascending // select new { t02, t01 }).ToList(); //多表联合查询完成-右关联 //执行存储过程 var result = te.TestProc().ToList(); //执行存储过程完成 }