Entity Framework 4.0 ObjectContext下的各种方法实践

1. AddObject

ExpandedBlockStart.gif View Code
 1         private   static   void  AddObjectDemo1()
 2          {
 3              Console.WriteLine( " --------Begin AddObjectDemo1 " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                  Customers addCustomer  =  edm.Customers.FirstOrDefault(c  =>  c.CustomerID  ==   " 001 " );
 7                   if  (addCustomer  ==   null )
 8                  {
 9                      Customers customer  =   new  Customers { CustomerID  =   " 001 " , CompanyName  =   " HongFan "  };
10                      edm.AddObject( " Customers " , customer);
11                       int  result  =  edm.SaveChanges();
12                  }
13                  Console.WriteLine( " CustomerID={0}, CompanyName={1} " , addCustomer.CustomerID, addCustomer.CompanyName);
14              }
15              Console.WriteLine( " --------End AddObjectDemo1 " );
16          }

 

2. AddObject 带映射关系

ExpandedBlockStart.gif View Code
 1           private   static   void  AddObjectDemo2()
 2          {
 3              Console.WriteLine( " --------Begin AddObjectDemo2 " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                  Categories cate1  =   new  Categories { CategoryName  =   " Category1 "  };
 7                  Categories cate2  =   new  Categories { CategoryName  =   " Category2 "  };
 8                  Products product1  =   new  Products { ProductName  =   " Category1--Product1 " , Discontinued  =   true  };
 9                  Products product2  =   new  Products { ProductName  =   " Category2--Product2 " , Discontinued  =   false  };
10 
11                  cate1.Products.Add(product1);
12                  cate2.Products.Add(product2);
13 
14                  edm.Categories.AddObject(cate1);
15                  edm.Categories.AddObject(cate2);
16 
17                  edm.SaveChanges();
18 
19                  Console.WriteLine( " Linq2Entities... " );
20 
21                  var categories  =  from category  in  edm.Categories
22                                    where  category.CategoryName  ==   " Category1 "   ||  category.CategoryName  ==   " Category2 "
23                                   select category;
24 
25                   foreach  (var category  in  categories)
26                  {
27                      Console.WriteLine( " 分类:{0} " , category.CategoryName);
28                       foreach  (var product  in  category.Products)
29                      {
30                          Console.WriteLine( " 产品:{0} " , product.ProductName);
31                      }
32                  }
33 
34                  Console.WriteLine( " EntitySQL... " );
35 
36                  var esql  =   @" select value c from Categories as c where c.CategoryName='Category2' or c.CategoryName='Category2' " ;
37                  var categoriesESQL  =  edm.CreateQuery < Categories > (esql);
38 
39                   foreach  (var category  in  categories)
40                  {
41                      Console.WriteLine( " 分类:{0} " , category.CategoryName);
42                       foreach  (var product  in  category.Products)
43                      {
44                          Console.WriteLine( " 产品:{0} " , product.ProductName);
45                      }
46                  }
47              }
48              Console.WriteLine( " --------End AddObjectDemo2 " );
49          }

 

3. DeleteObject

ExpandedBlockStart.gif View Code
 1         private   static   void  DeleteObjectDemo()
 2          {
 3              Console.WriteLine( " --------Begin DeleteObjectDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                  Customers customer  =  edm.Customers.FirstOrDefault(c  =>  c.CustomerID  ==   " 001 " );
 7                   if  (customer  !=   null )
 8                  {
 9                      edm.DeleteObject(customer);
10                       int  result  =  edm.SaveChanges();
11                  }
12 
13                  customer  =  edm.Customers.FirstOrDefault(c  =>  c.CustomerID  ==   " 001 " );
14                   if  (customer  ==   null )
15                  {
16                      Console.WriteLine( " CustomerID为001的客户已被删除成功。 " );
17                  }
18                   else
19                  {
20                      Console.WriteLine( " CustomerID={0},CompanyName={1} " , customer.CustomerID, customer.CompanyName);
21                  }
22              }
23              Console.WriteLine( " --------End DeleteObjectDemo " );
24          }

 

4. SaveChanges

ExpandedBlockStart.gif View Code
 1         private   static   void  SaveChangesDemo()
 2          {
 3              Console.WriteLine( " --------Begin SaveChangesDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                  Customers customer  =  edm.Customers.FirstOrDefault(c  =>  c.CustomerID  ==   " 001 " );
 7                   if  (customer  !=   null )
 8                  {
 9                      customer.CompanyName  =   " HongFanKeJi " ;
10                       int  result  =  edm.SaveChanges();
11                      Customers updateC  =  edm.Customers.FirstOrDefault(c  =>  c.CustomerID  ==   " 001 " );
12                      Console.WriteLine( " CustomerID={0}, CompanyName={1} " , updateC.CustomerID, updateC.CompanyName);
13                  }
14              }
15              Console.WriteLine( " --------End SaveChangesDemo " );
16          }

 

5. ExecuteStoreCommand

ExpandedBlockStart.gif View Code
 1         private   static   void  ExecuteStoreCommandDemo()
 2          {
 3              Console.WriteLine( " --------Begin ExecuteStoreCommandDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   @" insert into Customers(CustomerID, CompanyName) values(@CustomerID, @CompanyName) " ;
 7                  var parm  =   new  DbParameter[] {
 8                       new  SqlParameter { ParameterName  =   " CustomerID " , Value  =   " 002 "  },
 9                       new  SqlParameter { ParameterName  =   " CompanyName " , Value  =   " GuangzhouHongFan "  }
10                  };
11 
12                  Customers customer  =  edm.Customers.FirstOrDefault(c  =>  c.CustomerID  ==   " 002 " );
13                   if  (customer  !=   null )
14                  {
15                      edm.Customers.DeleteObject(customer);
16                      edm.SaveChanges();
17                  }
18                   int  rowCount  =  edm.ExecuteStoreCommand(strSql, parm);
19                  Console.WriteLine( " {0} rows inserted " , rowCount.ToString());
20 
21                  Customers customerInserted  =  edm.Customers.FirstOrDefault(c  =>  c.CustomerID  ==   " 002 " );
22                  Console.WriteLine( " CustomerID={0}, CompanyName={1} " , customerInserted.CustomerID, customerInserted.CompanyName);
23              }
24              Console.WriteLine( " --------End ExecuteStoreCommandDemo " );
25          }

 

6.ExecuteStoreQuery

ExpandedBlockStart.gif View Code
 1         private   static   void  ExecuteStoreQueryDemo()
 2          {
 3              Console.WriteLine( " --------Begin ExecuteStoreQueryDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select * from Customers where CompanyName=@CompanyName " ;
 7                  var parm  =   new  DbParameter[] {
 8                       new  SqlParameter { ParameterName  =   " CompanyName " , Value  =   " GuangzhouHongfan " }
 9                  };
10                  var customers  =  edm.ExecuteStoreQuery < Customers > (strSql, parm);
11 
12                   foreach  (var customer  in  customers)
13                  {
14                      Console.WriteLine( " CustomerID={0}, CompanyName={1} " , customer.CustomerID, customer.CompanyName);
15                  }
16              }
17              Console.WriteLine( " --------End ExecuteStoreQueryDemo " );
18          }

 

7. Execute

ExpandedBlockStart.gif View Code
 1         private   static   void  ExecuteDemo()
 2          {
 3              Console.WriteLine( " --------Begin ExecuteDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10 " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  ObjectResult < Customers >  results  =  query.Execute(MergeOption.NoTracking);
10                   foreach  (Customers c  in  query)
11                  {
12                      Console.WriteLine( " CustomerID={0}, CompanyName={1} " , c.CustomerID, c.CompanyName);
13                  }
14 
15                  Console.WriteLine(Environment.NewLine);
16 
17                   foreach  (Customers c  in  results)
18                  {
19                      Console.WriteLine( " CustomerID={0}, CompanyName={1} " , c.CustomerID, c.CompanyName);
20                  }
21              }
22              Console.WriteLine( " --------End ExecuteDemo " );
23          }

 

8. ToTraceString

ExpandedBlockStart.gif View Code
 1         private   static   void  ToTraceStringDemo()
 2          {
 3              Console.WriteLine( " --------Begin ToTraceStringDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10 " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  Console.WriteLine( " The sql for this statement: select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10\r\n " );
10                  Console.WriteLine(query.ToTraceString());
11              }
12              Console.WriteLine( " --------End ToTraceStringDemo " );
13          }

 

9. Where

ExpandedBlockStart.gif View Code
 1         private   static   void  WhereDemo()
 2          {
 3              Console.WriteLine( " --------Begin WhereDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10 " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  query.Name  =   " a " ;
10                  query  =  query.Where( " a.CustomerID=@customerID " new  ObjectParameter( " customerID " " 001 " ));
11 
12                   foreach  (Customers c  in  query)
13                  {
14                      Console.WriteLine( " CustomerID={0}, CompanyName={1} " , c.CustomerID, c.CompanyName);
15                  }
16              }
17              Console.WriteLine( " --------End WhereDemo " );
18          }

 

10. FirstOrDefault

ExpandedBlockStart.gif View Code
 1         private   static   void  FirstOrDefaultDemo()
 2          {
 3              Console.WriteLine( " --------Begin FirstOrDefaultDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10 " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  
10                   // 如果没有符合条件的数据,那么将会抛出异常。
11                  Customers c1  =  query.First();
12 
13                   // 如果没有符合条件的数据,那么将返回null。
14                  Customers c2  =  query.FirstOrDefault();
15 
16                  Console.WriteLine( " CustomerID={0}, CompanyName={1} " , c1.CustomerID, c1.CompanyName);
17                  Console.WriteLine( " CustomerID={0}, CompanyName={1} " , c2.CustomerID, c2.CompanyName);
18              }
19              Console.WriteLine( " --------End FirstOrDefaultDemo " );
20          }

 

11. Distinct

ExpandedBlockStart.gif View Code
 1         private   static   void  DistinctDemo()
 2          {
 3              Console.WriteLine( " --------Begin DistinctDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c.City from NorthwindEntities.Customers as c order by c.CustomerID limit 10 " ;
 7 
 8                  ObjectQuery < string >  query  =  edm.CreateQuery < string > (strSql);
 9                  query  =  query.Distinct();
10 
11                   foreach  ( string  city  in  query)
12                  {
13                      Console.WriteLine( " City:{0} " , city);
14                  }
15              }
16              Console.WriteLine( " --------End DistinctDemo " );
17          }

 

12. Include

ExpandedBlockStart.gif View Code
 1           private   static   void  IncludeDemo()
 2          {
 3              Console.WriteLine( " --------Begin IncludeDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c where c.CustomerID='001' " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  query  =  query.Include( " Orders " );
10 
11                   foreach  (Customers customer  in  query)
12                  {
13                      Console.WriteLine( " CustomerID={0}, OrderCount={1} " , customer.CustomerID, customer.Orders.Count);
14                  }
15 
16                  Console.WriteLine(query.ToTraceString());
17              }
18              Console.WriteLine( " --------End IncludeDemo " );
19          }

 

13. OrderBy

ExpandedBlockStart.gif View Code
 1           private   static   void  OrderByDemo()
 2          {
 3              Console.WriteLine( " --------Begin OrderByDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10 " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  query.OrderBy( " it.country asc, it.city asc " );
10 
11                   //  也可以这样写
12                   // query.OrderBy("it.country asc");
13                   // query.OrderBy("it.city asc");
14 
15                   foreach  (Customers c  in  query)
16                  {
17                      Console.WriteLine( " CustomerID={0}, CompanyName={1} " , c.CustomerID, c.CompanyName);
18                  }
19              }
20              Console.WriteLine( " --------End OrderByDemo " );
21          }

 

14. Select

ExpandedBlockStart.gif View Code
 1           private   static   void  SelectDemo()
 2          {
 3              Console.WriteLine( " --------Begin SelectDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10 " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  ObjectQuery < DbDataRecord >  records  =  query.Select( " it.customerid, it.country " );
10 
11                   foreach  (DbDataRecord c  in  records)
12                  {
13                      Console.WriteLine( " CustomerID={0}, Country={1} " , c[ " customerid " ], c[ " country " ]);
14                  }
15                  Console.WriteLine(records.ToTraceString());
16              }
17              Console.WriteLine( " --------End SelectDemo " );
18          }

 

15. SelectValue

ExpandedBlockStart.gif View Code
 1           private   static   void  SelectValueDemo()
 2          {
 3              Console.WriteLine( " --------Begin SelectValueDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10 " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  ObjectQuery < string >  records  =  query.SelectValue < string > ( " it.customerid " );
10 
11                   foreach  ( string  c  in  records)
12                  {
13                      Console.WriteLine( " CustomerID={0} " , c);
14                  }
15                  Console.WriteLine(records.ToTraceString());
16              }
17              Console.WriteLine( " --------End SelectValueDemo " );
18          }

 

16. Skip、Top

ExpandedBlockStart.gif View Code
 1           private   static   void  SkipTopDemo()
 2          {
 3              Console.WriteLine( " --------Begin SkipTopDemo " );
 4               using  (var edm  =   new  NorthwindEntities())
 5              {
 6                   string  strSql  =   " select value c from NorthwindEntities.Customers as c order by c.CustomerID " ;
 7 
 8                  ObjectQuery < Customers >  query  =  edm.CreateQuery < Customers > (strSql);
 9                  query  =  query.Skip( " it.customerid asc " " 10 " );  //  排序,跳过条数
10                  query  =  query.Top( " 10 " );
11 
12                   foreach  (Customers c  in  query)
13                  {
14                      Console.WriteLine( " CustomerID={0} " , c.CustomerID);
15                  }
16                  Console.WriteLine(query.ToTraceString());
17              }
18              Console.WriteLine( " --------End SkipTopDemo " );
19          }

 

转载于:https://www.cnblogs.com/myself/archive/2011/02/26/1965758.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
08-10
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值