【问题标题】:Entity Framework 5 return SQL stored procedure single result efficiency vb.netEntity Framework 5 返回 SQL 存储过程单结果效率 vb.net
【发布时间】:2016-08-10 06:09:48
【问题描述】:

如果我可以访问一个名为 procResults 的存储过程,它只返回一行数据,那么选择几个单个元素而不是整行是否会导致对数据库的多个查询?例如,如果我有:

Dim MyEntity As WhateverEntities = New WhateverEntities 
Dim ResultName as String = MyEntity.procResults.Single().FullName
Dim ResultEmail as String = MyEntity.procResults.Single().Email

这是否会导致对数据库的两个实际查询,或者初始查询是否将整行存储在内存中并只为我提供我要求的两列?理想情况下,我只想返回这两列而不是整行,但不知道最有效的方法是什么。

【问题讨论】:

  • 恕我直言,EF缓存结果的唯一部分是您使用Find时。 Note that DbSet [...] always create queries against the database and will always involve a round trip to the database even if the entities returned already exist in the context。查找:The Find method [...] uses the primary key value to attempt to find an entity tracked by the context. If the entity is not found in the context then a query will be sent to the database to find the entity there.

标签: sql-server vb.net entity-framework stored-procedures


【解决方案1】:

我无法从VB.Net 方面回答,但你可以做的是:

您可以使用 SQL Server Profiler 来跟踪执行代码时数据库中发生的情况。你会看到你的存储过程被执行了多少次。

这是一篇 MSDN 文章,解释如何使用这个工具:How To: Use SQL Profiler

【讨论】:

    【解决方案2】:
    Dim MyEntity As WhateverEntities = New WhateverEntities 
    Dim ResultName as String 
    Dim ResultEmail as String 
    
    With MyEntity.procResults.Single()
            ResultName = .FullName
            ResultEmail = .Email
    End With
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多