Home
Options

EF Eager Loading - related tables.

Hi,

I've noticed that Linqpad recognizes foreign keys. However, does it support Entity Frameworks "Eager Loading" of related tables ?

For example, a query like Organisations.Include( p => p.PhoneNumber) doesn't seem to work in Linqpad

Instead, I need to use a linq join.

Paul

Comments

  • Options

    Which version of LINQPad are you using?

    The following works for me in LINQPad 6, using an automatically generated model with SQL Server and EF Core 3.1.4 (AdventureWorks):

    from p in Products.Include (p => p.ProductModel)
    select new { p.Name, ProductModel = p.ProductModel.Name }
    

    Here's the SQL generated:

    SELECT [p].[Name], [p0].[Name] AS [ProductModel]
    FROM [Production].[Product] AS [p]
    LEFT JOIN [Production].[ProductModel] AS [p0] ON [p].[ProductModelID] = [p0].[ProductModelID]
    
  • Options

    Thanks Joe,
    Would you mind including the full code you used.
    I'm clearly doing something wrong.
    Paul

  • Options

    That is the full code. Click Add connection, choose Entity Framework Core (multiprovider), choose SQL Server and point it to AdventureWorks.

    Then run the following query using the default language (C# Expression):

    from p in Products.Include (p => p.ProductModel)
    select new { p.Name, ProductModel = p.ProductModel.Name }
    
  • Options

    Arggghhhh ! My apologies Joe and thanks for your patience.
    I completely missed that.

    I ended up writing a whole EF DbContext in Linqpad - which worked fine, but was more work, than I planned.

    It's been so long since I've used the Add Connection dialog, I'd forgotten about the other drivers, having dragged and dropped connections in.

    Paul

  • Options

    No worries, enjoy the product!

Sign In or Register to comment.