concept SQL in category entity framework

appears as: SQL, SQL, The SQL, An SQL
Entity Framework Core in Action

This is an excerpt from Manning's book Entity Framework Core in Action.

  • Use a query type in a FromSQL method. This allows you to write SQL that maps to a class that isn’t an entity class.
  • That’s why I spend time in this chapter showing how EF Core works on the inside, and the SQL it produces. The more you understand about what EF Core is doing, the better equipped you’ll be to write good EF Core code, and more important, know what to do when it doesn’t work.

    Listing 2.8 Select query that includes a non-SQL command, string.Join
    var book = context.Books
        .Select(p => new
        {
            p.BookId,    #1  
            p.Title,    #1  
            AuthorsString = string.Join(", ",  #2  
                p.AuthorsLink    #1  
                .OrderBy(q => q.Order)    #1  
                .Select(q => q.Author.Name)),  #1  
        }
        ).First();
    
    #1   These parts of the select can be converted to SQL and run on the server.
    #2   string.Join is executed on the client in software.

    Figure 2.8 Some parts of the query are converted to SQL and run in the SQL server, and another part, in this case string.Join, has to be done client-side by EF Core before the combined result is handed back to the application code.

    c02-8.png
    Entity Framework 4 in Action

    This is an excerpt from Manning's book Entity Framework 4 in Action.

    Even though LINQ to Entities operates on objects, it must still be translated into the SQL that’s finally launched against the database. Many of the LINQ methods or overloads can’t be represented in SQL. For instance, the ElementAt method isn’t supported but is still syntactically valid. A call to this method won’t cause a compile-time error but will produce a NotSupportedException at runtime. Similarly, the overload of the Where method that accepts an integer as the second parameter can’t be translated to SQL, so its use causes only a runtime exception. Apart from unsupported methods, you’ll encounter other cases where the compile-time checking isn’t enough, but they’re pretty rare and are well documented in MSDN.

    Figure 3.1. Queries written against classes are transformed into SQL.

    One of the most important features of Entity Framework is the LINQ to Entities query dialect. LINQ to Entities is a specialized version of LINQ that operates on Entity Framework models. LINQ to Entities isn’t a LINQ to SQL competitor—it’s the Entity Framework’s main query language, whereas LINQ to SQL is a full-featured O/RM that shipped with the .NET Framework v3.5.

    This SQL is close to what you would have written manually. The SQL Server team has closely collaborated with the Entity Framework developers to make the generated SQL as performant as possible. The SQL generated by Entity Framework isn’t always the best, due to the generic nature of the SQL generator, but most of the time you can live with it.