in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Entity Framework: Include() not working with a "let" sub query

Last post 06-22-2009 10:55 AM by trevhunter. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 06-01-2009 3:03 PM

    Entity Framework: Include() not working with a "let" sub query

    Hi,

    I'm having a spot of trouble with the Linq-To-Entities version of SQLite. If I use the "Include()" method to eager fetch a related property, it normally works fine. However, if I include a "let" statement in the query, the "Include()" call is ignored (and this can be verified by viewing the SQL in the ToTraceString() method.)

    The workaround for the moment seems to be to pre-load the related entity into the data context... the property is then populated correctly. Below is some source code and I've attached a sample project to demonstrate this problem. Any help is much appreciated.

    Regards,

    Trev.

    Code:

    // set up the source of the query to include the customer
    // on orders
    var source = db.Order.Include("Customer");

    // Build on the query
    var query = from o in source
    let suppliers = o.Suppliers.Any(s=>s.SupplierId == 1)
    where o.OrderId == 1
    select o;

    // Select the result and check the customer property.
    // It will be null
    // (it won't be null if the suppliers clause of the
    // (query above is commented out)
    var order = query.First();
    Console.WriteLine("First Try Customer Null: {0}", object.ReferenceEquals(null, order.Customer));

    // try again, but this time, evaluate the
    // "source" first.
    var dummy = source.ToList();

    // This time, the customer property won't be null.
    order = query.First();
    Console.WriteLine("Second Try Customer Null: {0}",

    object.ReferenceEquals(null, order.Customer));

    Filed under: ,
  • 06-18-2009 4:00 PM In reply to

    Re: Entity Framework: Include() not working with a "let" sub query

    Hows it work on Sql Server?  Properly?  Can you show me the TraceString() for both cases on Sql Server?

     

  • 06-19-2009 7:07 AM In reply to

    Re: Entity Framework: Include() not working with a "let" sub query

    I tried to reproduce the table schema on Sql Server and got several errors.

    First, these foreign keys failed because [OrderId] and [SupplierId] in [Supplier_Orders] are not unique:
    ALTER TABLE [Order] ADD CONSTRAINT [FK_Order_OrderId_Supplier_Orders_OrderId] FOREIGN KEY ([OrderId]) REFERENCES [Supplier_Orders] ([OrderId])

    ALTER TABLE [Supplier] ADD CONSTRAINT [FK_Supplier_SupplierId_Supplier_Orders_SupplierId] FOREIGN KEY ([SupplierId]) REFERENCES [Supplier_Orders] ([SupplierId])

    Next, this foreign key references a column that doesn't exist, but since SQLite only parses and doesn't enforce, I suspect it was a schema change somewhere that allowed it to happen:

    ALTER TABLE [Supplier_Orders] ADD CONSTRAINT [FK_Supplier_Orders_OrderId_Order_Id] FOREIGN KEY ([OrderId]) REFERENCES [Order] ([Id])

    So I'm not sure how you want to fix those errors, but I suspect things will work better once you have.

     

  • 06-19-2009 7:23 AM In reply to

    Re: Entity Framework: Include() not working with a "let" sub query

    One last thing ... when I did correct two of the instances where columns had been renamed, then I recreated the EDMX file, I got the following messages:

    Message 1 The relationship 'FK_Customer_0' has columns that are not part of the key of the table on the primary side of the relationship which is not supported, the relationship was excluded. E:\src\SQLiteEFIncludeProblem\SQLiteEFIncludeProblem\Model1.edmx 0
    Message 2 The relationship 'FK_Order_0' has columns that are not part of the key of the table on the primary side of the relationship which is not supported, the relationship was excluded. E:\src\SQLiteEFIncludeProblem\SQLiteEFIncludeProblem\Model1.edmx 0
    Message 3 The relationship 'FK_Supplier_0' has columns that are not part of the key of the table on the primary side of the relationship which is not supported, the relationship was excluded. E:\src\SQLiteEFIncludeProblem\SQLiteEFIncludeProblem\Model1.edmx 0

    So I suspect this is why your let statement isn't working -- the association between suppliers and orders bridged by the suppler_order table isn't being made.

  • 06-19-2009 7:45 AM In reply to

    Re: Entity Framework: Include() not working with a "let" sub query

    I finally got this into Sql Server by simplifying the schema and removing the invalid foreign keys.  However, when I ran your code against Sql Server, I got the exact same results and behavior as with SQLite:

    -- Original table schema
    CREATE TABLE [Customer] (
    [CustomerId] int PRIMARY KEY NOT NULL,
    [Name] varchar(50)
    );

    -- Original table schema
    CREATE TABLE [Order] (
    [OrderId] int PRIMARY KEY NOT NULL,
    [CustomerId] int,
    [OrderTotal] float,
    CONSTRAINT [FK_Order_CustomerId_Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customer] ([CustomerId])
    );

    -- Original table schema
    CREATE TABLE [Supplier] (
    [SupplierId] int PRIMARY KEY NOT NULL,
    [Name] varchar(50)
    );

    -- Original table schema
    CREATE TABLE [Supplier_Orders] (
    [SupplierId] int NOT NULL,
    [OrderId] int NOT NULL,
    CONSTRAINT [PK_Supplier_Orders] PRIMARY KEY ([SupplierId], [OrderId]),
    CONSTRAINT [FK_Supplier_Orders_0] FOREIGN KEY ([OrderId]) REFERENCES [Order] ([OrderId]),
    CONSTRAINT [FK_Supplier_Orders_1] FOREIGN KEY ([SupplierId]) REFERENCES [Supplier] ([SupplierId])
    );

     

  • 06-22-2009 10:55 AM In reply to

    Re: Entity Framework: Include() not working with a "let" sub query

    Thanks for looking into this Robert... apologies for the delay in getting back to you. I'll see about logging an issue with MS Feedback and post the link to the issue here.
Page 1 of 1 (6 items)
Powered by Community Server (Commercial Edition), by Telligent Systems