Prefetch adds entity twice if it appears elsewhere in query

Posts   
 
    
yowl
User
Posts: 271
Joined: 11-Feb-2008
# Posted on: 15-Jul-2021 21:02:51   

LLBLGen Framework 5.8.2, Adapter with SQL Server and AdventureWorksLT

This test fails as the detail entity, of which there is one, gets into the SalesOrderHeader.SalesOrderDetails twice, no doubt due to the slightly odd prefetch construct. In this scenario the key for the detail is given, so the detail is the root entity in the prefetch, but all the details under the header are required. Having 2 elements in the SalesOrderHeader.SalesOrderDetails collection seems wrong to me, but maybe this is by design?

        [Test]
        public void SameInPrefetch()
        {
            var salesOrderHeader = new SalesOrderHeaderEntity
            {
                SalesOrderId = 1,
                OrderDate = DateTime.Today,
                DueDate = DateTime.Today,
                CustomerId = 1,
                ShipMethod = "",
            };
            salesOrderHeader.SalesOrderDetails.Add(new SalesOrderDetailEntity
            {
                OrderQty = 1,
                UnitPrice = 1,
                ProductId = 680,
            });

            var daa = new DataAccessAdapter("Server=tcp:localhost;Database=AdventureWorksLT2019;Integrated Security=SSPI;");
            daa.SaveEntity(salesOrderHeader, true, true);

            var meta = new LinqMetaData(daa);
            var detail = (from d in meta.SalesOrderDetail
                where d.Rowguid == salesOrderHeader.SalesOrderDetails[0].Rowguid
                select d).WithPath(de => de.Prefetch<SalesOrderHeaderEntity>(dp => dp.SalesOrderHeader)
                .SubPath(headerP => headerP.Prefetch(h => h.SalesOrderDetails))).Single();

            Assert.AreEqual(1, detail.SalesOrderHeader.SalesOrderDetails.Count);
        }
Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 16-Jul-2021 01:48:30   

Please try adding a context to the LinqMetaData.

meta.ContextToUse = new Context();
yowl
User
Posts: 271
Joined: 11-Feb-2008
# Posted on: 16-Jul-2021 01:55:36   

Yes, that's what I needed. Thanks.