Where Query Builder Method with Entity SQL parameters Error in Entity Framework

entity framework raw sql query parameters
entity framework execute sql query with parameters
entity framework core executesqlcommand
entity framework executesqlcommand
get sql query from entity framework core
entity framework core 2.1 stored procedure

I am trying to follow an example in Julia Lerman's book Programming Entity Framework 2nd edition and the following code wont work for me:

ObjectQuery<Contact> contacts = context.Contacts
                                .Where("it.FirstName = 'Robert'")

I get the following error:

Argument2: can not convert from 'string' to 'System.Linq.Expresions.Expression>'.

How can I access Where the Query Builder Method of ObjectQuery? I am using Entity Framework 6, the examples in the book are using Entity Framework 4. My context class inherits from DbContext, in the example in the book the context class inherits from ObjectContext.

Are query builder methods still used in Entity Framework 6?

My context class inherits from DbContext, in the example in the book the context class inherits from ObjectContext.

First you have to change your context from DbContext into an ObjectContext like this:

var objectContext = (context as IObjectContextAdapter).ObjectContext;

Now you can use use the Query Builder Method like this:

        var query = objectContext.CreateObjectSet<Customer>().Where("it.FirstName='Robert'").OrderBy("it.LastName");

Here is the whole example:

private static void OrderByAndWhereUsingEntitySQL_AndQueryBuilderMethods()
    using (var context = new AWEntities())
    {//using Entity SQL with helper Query Builder Methods
        //dbContext does not have a Where method that accepts esql string statements
        //get to the dbContext's ObjectContext class
        var objectContext = (context as IObjectContextAdapter).ObjectContext;
        //now we can use the Where method that understands esql string statements
        var query = objectContext.CreateObjectSet<Customer>().Where("it.FirstName='Robert'").OrderBy("it.LastName");
        //execute the query
        var customers = query.ToList();
        foreach (var cust in customers)
            Debug.WriteLine(cust.LastName.Trim() + ", " + cust.FirstName);

Querying in Entity Framework, How do I view the SQL generated by the entity framework? This topic shows how to execute an Entity SQL query that has parameters by using an EntityCommand object. To run the code in this example. Add the AdventureWorks Sales Model to your project and configure your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard.

If you want to use a string, you need to use parameterization, and Where requires a second argument with the value.

I think just using straight LINQ with a lambda expression is cleaner.


ObjectQuery<Contact> contacts =
context.Contacts.Where("it.FirstName = @firstname",
new ObjectParameter("firstname", firstName));

How do I view the SQL generated by the Entity Framework?, what the query would be at any given point for any LINQ query. Entity Framework provides support for executing raw SQL queries against the database. This feature is available in Entity Framework Core as well. Such raw SQL queries can return entity types or query types (from EF Core 2.1). This article presents a discussion on how we can work with raw SQL queries in Entity Framework Core.

The standard Linq queries do not provide an overload for Where with type string

Microsoft provides a NuGet library for parsing strings into Lambda expressions that can then be used in Linq queries called System.Linq.Dynamic which can be found here:


Raw SQL Queries, How do I turn off lazy loading in Entity Framework? The following methods can be used to execute raw SQL queries to the database using Entity Framework 6.x: DbSet.SqlQuery() DbContext.Database.SqlQuery() DbContext.Database.ExecuteSqlCommand() DbSet.SqlQuery() Use the DbSet.SqlQuery() method to write raw SQL queries which return entity instances. The resulted entities will be tracked by the

Raw SQL Queries, The following example passes a single parameter to a stored procedure by including a parameter placeholder in the SQL query string and  Let's just suppose I have a valid need for directly executing a sql command in Entity Framework. I am having trouble figuring out how to use parameters in my sql statement. The following example (not my real example) doesn't work.

Executing Raw SQL Queries using Entity Framework, Entity Framework allows you to query using LINQ with your entity apply equally to models created with Code First and the EF Designer. Writing SQL queries for entities. The SqlQuery method on DbSet allows a raw SQL query to be written You can also pass parameters to a stored procedure using the  @TomHalladay no risk of SQL injection - the method will still quote and escape the parameters based on their type, the same as the @ style params. So for a string parameter you would use "SELECT * FROM Users WHERE email={0}" without quotes in your statement.

Executing Raw SQL Queries using Entity Framework Core, Luckily, EF allows you to execute raw queries easily. executed against the Northwind database using the EF context generated by the designer. The SqlQuery() method accepts two parameters - the SELECT query to be  Starting with Entity Framework 6, anytime Entity Framework sends a command to the database this command can be intercepted by application code. This is most commonly used for logging SQL, but can also be used to modify or abort the command. Specifically, EF includes: A Log property for the context similar to DataContext.Log in LINQ to SQL