EF 的 CURD 操作
2018-06-17 22:06:34来源:未知 阅读 ()
EF 的 CURD 操作
这里采用了数据库 Northwind,下载地址:https://northwinddatabase.codeplex.com/
增
1 /// <summary> 2 /// 添加 3 /// </summary> 4 /// <returns></returns> 5 public static int Add() 6 { 7 using (var db = new NorthwindEntities()) 8 { 9 var customer = new Customers() 10 { 11 CustomerID = "fanguza", 12 Address = "地球", 13 City = "广东", 14 Phone = "13800138000", 15 CompanyName = "博客园", 16 ContactName = "反骨仔" 17 }; 18 19 //方法一 20 //db.Customers.Add(customer); 21 22 //方法二 23 var entry = db.Entry(customer); 24 entry.State = EntityState.Added; 25 26 return db.SaveChanges(); 27 } 28 }
删
1 /// <summary> 2 /// 删除 3 /// </summary> 4 /// <returns></returns> 5 static int Delete() 6 { 7 using (var db = new NorthwindEntities()) 8 { 9 var customer = new Customers() 10 { 11 CustomerID = "fanguzi" 12 }; 13 14 db.Customers.Attach(customer); 15 db.Customers.Remove(customer); 16 17 return db.SaveChanges(); 18 } 19 }
改
1 /// <summary> 2 /// 编辑(修改) 3 /// </summary> 4 /// <returns></returns> 5 static int Edit() 6 { 7 using (var db = new NorthwindEntities()) 8 { 9 var customer = db.Customers.FirstOrDefault(u => u.CustomerID == "fanguzai"); 10 11 if (customer == null) 12 { 13 return 0; 14 } 15 16 customer.ContactName = "erwuzai"; 17 return db.SaveChanges(); 18 } 19 }
查
1 /// <summary> 2 /// 查询 3 /// </summary> 4 static Customers Query() 5 { 6 using (var db = new NorthwindEntities()) 7 { 8 var query = 9 db.Customers.Where(u => u.ContactName == "fanguza").OrderBy(u => u.ContactName).Take(1); 10 11 return query.FirstOrDefault(); 12 } 13 }
分页
1 public IQueryable<Customers> GetPageOfCustomerses<TKey>(int pageIndex, int pageSize, 2 Expression<Func<Customers, bool>> wherExpression, 3 Expression<Func<Customers, TKey>> orderByExpression) 4 { 5 using (var db = new NorthwindEntities()) 6 { 7 //分页前需要排序 8 return db.Customers.Where(wherExpression).OrderBy(orderByExpression).Skip((pageIndex - 1) * pageSize).Take(pageSize); 9 } 10 }
批量增加
1 static void BatchAdd() 2 { 3 using (var db = new NorthwindEntities()) 4 { 5 for (var i = 0; i < 50; i++) 6 { 7 var customer = new Customers() 8 { 9 CustomerID = "fanguzai" + i, 10 Address = "中国", 11 City = "广州", 12 Phone = "13800138000" + i, 13 CompanyName = "阳春一中", 14 ContactName = "反骨仔" + i 15 }; 16 17 db.Customers.Add(customer); 18 } 19 20 db.SaveChanges(); 21 } 22 }
其它
1 public IQueryable<Customers> GetCustomerses<TKey>(Expression<Func<Customers, bool>> whereExpression, 2 Expression<Func<Customers, TKey>> orderExpression) 3 { 4 using (var db = new NorthwindEntities()) 5 { 6 return db.Customers.Where(whereExpression).OrderBy(orderExpression); 7 } 8 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PHP 使用 Swoole - TaskWorker 实现异步操作 Mysql 2019-09-23
- 使用 Shell 在多服务器上批量操作 2019-09-23
- MySQL性能优化的20条经验 2019-09-23
- Laravel实现找回密码及密码重置,详细操作 2019-08-31
- MySQL语句大全,每天跟着操作一遍,不信你不懂! 2019-08-29
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash