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

时间:2022-02-18 18:28:21

1. AddObject

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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 带映射关系

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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

Entity Framework 4.0 ObjectContext下的各种方法实践Entity Framework 4.0 ObjectContext下的各种方法实践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          }