可以使用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 Blog。

相关文章: