EF 的 CURD 操作

2018-06-18 01:12:48来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:接口调试,HttpWebRequest和HttpWebResponse使用,接口回调处理

下一篇:visual studio 2017无建模项目?