EF 的 CURD 操作

Posted 反骨仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF 的 CURD 操作相关的知识,希望对你有一定的参考价值。

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         }

 

以上是关于EF 的 CURD 操作的主要内容,如果未能解决你的问题,请参考以下文章

自定义ValidateEntity实体验证及DB层面Interception拦截EF中的Execute

EF添加关联的提示问题:映射从第 260 行开始的片段时有问题:

Oracle 对表的基本CURD操作

HIbernate CURD操作

Laravel框架数据库CURD操作连贯操作使用方法

EF6 自定义迁移表名