可以使用ExecuteStoreQuery<T>方法:

   1:          [Test]
   2:          public void ExecuteTSQLInEF4()
   3:          {
   4:              using (var context = new AdventureWorksEntities())
   5:              {
   6:                  var query = context.ExecuteStoreQuery<Employee>("SELECT TOP 10 * FROM HumanResources.Employee");
   7:                  foreach (var employee in query)
   8:                  {
   9:                      Console.WriteLine(employee.Title);
  10:                  }
  11:              }
  12:              Console.ReadLine();
  13:          }

T是Genric,可参考MSDN

使用ExecuteStoreCommand 方法:

这个更加灵活,你可以执行Update,Insert语句

   1:          [Test]
   2:          public void ExecuteTSQLInEF4_Part2()
   3:          {
   4:              using (var context = new AdventureWorksEntities())
   5:              {
   6:                  var query = context.ExecuteStoreCommand("SELECT TOP 10 * FROM HumanResources.Employee");
   7:                  foreach (var employee in query)
   8:                  {
   9:                      Console.WriteLine(employee.Title);
  10:                  }
  11:              }
  12:              Console.ReadLine();
  13:          }

代码很简单,您只要对Entity Framework有一定了解,可以看懂上面的代码。EF4中ObjectContext提供了比较多的方法供我们使用。

希望这篇POST对您有帮助!


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

posted on 2010-07-22 13:07  PetterLiu  阅读(2044)  评论(2编辑  收藏  举报