• 在Entity Framework4中执行Tsql语句


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

  • 相关阅读:
    react 踩坑第一天
    vue-cli+axios+跨域
    vuex 进阶 mapState mapGetter mapActions mapMutations
    vue Bus
    各种html布局
    echarts柱状图设置渐变色
    html设置一个当前时间
    css设置字体渐变色
    C++中指针与引用详解
    go-admin 开源后台管理系统
  • 原文地址:https://www.cnblogs.com/wintersun/p/1782975.html
Copyright © 2020-2023  润新知