Skip to content

Allow creation of a DbCommand from an EF IQueryable #19358

Closed

Description

@ajcvickers

See #19335

For example:

var query = context.Blogs.Where(b => b.Published);
var command = query.CreateDbCommand();

Things to note:

  • The command providers access to the SQL query string and parameters.
  • The created command is not the actual command that EF will use if the query is then executed. A new command will be created for this, but using the same mechanism.
  • The created command can likely be executed, but will be doing so outside of the normal EF environment and so may not have exactly the same behavior.

Activity

  1. added this to the 5.0.0 milestone on Dec 18, 2019
  2. self-assigned this
    on Dec 18, 2019
  3. modified the milestones: 5.0.0, on Mar 13, 2020
  4. Giorgi commented on Mar 16, 2020

    @Giorgi
    Contributor

    Looking at the Source of CreateDbCommand it looks like that the command must be executed before the cache is populated. Is there a way to get the command without actually executing the IQueryable ?

  5. ajcvickers commented on Mar 16, 2020

    @ajcvickers
    ContributorAuthor

    @Giorgi What "cache" are you referring to?

  6. Giorgi commented on Mar 16, 2020

    @Giorgi
    Contributor

    I mean this code:

    public virtual DbCommand CreateDbCommand()

  7. Giorgi commented on Mar 16, 2020

    @Giorgi
    Contributor

    The code that I linked to in my first comment calls source.Provider.Execute so I guess that is needed to populate the _relationalCommandCache

  8. ajcvickers commented on Mar 16, 2020

    @ajcvickers
    ContributorAuthor

    @Giorgi The code for CreateDbCommand does not execute the command. Also, as far as I can tell, it will populate/use the command cache.

  9. modified the milestones: , 5.0.0 on Nov 7, 2020
  10. removed their assignment
    on Sep 1, 2024
  11. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions