Not able to get Deep Load working

Mar 18, 2014 at 8:46 PM
Hello Le,

Thanks for providing this framework. I've downloaded the latest release and I'm using it as a starting point for a new project. I've been having an issue with not getting related entities to load properly. I looked in your Northwind app and I think I've identified the same issue in the test method DeepLoadProductWithSupplier(). In the test you look for the existence of product, but not for supplier. I added an assert for supplier and it failed. I'm pretty new to EF, so I'm not sure if I'm just missing something simple or if there's something deeper going on. Here's the updated test method:
        [TestMethod]
        public void DeepLoadProductWithSupplier()
        {
            using (IDataContextAsync northwindFakeContext = new NorthwindFakeContext())
            using (IUnitOfWork unitOfWork = new UnitOfWork(northwindFakeContext))
            {
                unitOfWork.Repository<Supplier>().Insert(new Supplier {SupplierID = 1, CompanyName = "Nokia", City = "Tampere", Country = "Finland", ContactName = "Stephen Elop", ContactTitle = "CEO"});
                unitOfWork.Repository<Product>().Insert(new Product {ProductID = 2, Discontinued = true, ProductName = "Nokia Lumia 1520", SupplierID = 1, ObjectState = ObjectState.Added});

                unitOfWork.SaveChanges();

                var product = unitOfWork.Repository<Product>().Find(2);
                var supplier = product.Supplier;

                Assert.IsNotNull(product);
                Assert.IsNotNull(supplier);
            }
        }
Thanks
Mar 18, 2014 at 9:19 PM
Hi Dlharrison,

Please use integration test for this, where you actually setup a database for your test to run against and destroy the database once they are completed. The mocks run against in-memory Observable collections and are not related as they would be in SQL Server, again they are mocked.

A Find(id) does not return the entire object graph, you will need to do Repository<Product>().Query(t => t.ProductId == 2).Include(t => t.Supplier).Select().First();
Marked as answer by lelong37 on 3/31/2014 at 10:24 PM
Mar 18, 2014 at 10:24 PM
Thanks for the response, the .Include() was what I was missing. In my service I added a GetByID() method to encapsulate the retrieving of the entire graph. Is that how/where you would recommend getting an entire object graph? In my case, it's a UserRole entity with SecurityComponents:

In UserRoleService.cs
        public UserRole GetByID(int userRoleID)
        {
            return this.Query(ur => ur.UserRoleID == userRoleID).Include(sc => sc.UserRoleSecurityComponents).Select().First();
        }
Mar 18, 2014 at 10:27 PM
Test

Regards,

Long Le
[email removed]


Mar 18, 2014 at 10:28 PM
You can default to lazy/eager loading = true in the DbContext, but that's a anti-practice, the way you've done it here is the best practice, you being deliberate about what you want to query especially when dealing with graphs which can sink both your web and sql server with high traffic.
Mar 18, 2014 at 11:53 PM
Thanks
Jul 26, 2014 at 5:35 PM
Hi,

I am definitely having issues with Deep Loading - have spent days on this issue and really struggling to resolve.
The statement I am using is same as above:
  var artist = _artistService.Query(a => a.ArtistID == id).Include(al => al.ArtistLinks).Select().First();

Where I simply want to get all ArtistLinks associated the current artist.

Could I have setup EF incorrectly?
I have ensured:
  • Lazy Loading and ProxyCreation is disabled.
The SQL being created looks fine:

SELECT
[Project1].[ArtistID] AS [ArtistID], 
[Project1].[ArtistName] AS [ArtistName], 
[Project1].[ArtistNickname] AS [ArtistNickname], 
[Project1].[ArtistTitle] AS [ArtistTitle], 
[Project1].[ArtistDescription] AS [ArtistDescription], 
[Project1].[EnableSoundCloudDisplay] AS [EnableSoundCloudDisplay], 
[Project1].[SoundCloudUrl] AS [SoundCloudUrl], 
[Project1].[EnableYouTubeDisplay] AS [EnableYouTubeDisplay], 
[Project1].[YouTubeUrl] AS [YouTubeUrl], 
[Project1].[EnableFlickrDisplay] AS [EnableFlickrDisplay], 
[Project1].[FlickrSet] AS [FlickrSet], 
[Project1].[Notes] AS [Notes], 
[Project1].[IsFeatured] AS [IsFeatured], 
[Project1].[FeatureYouTubeUrl] AS [FeatureYouTubeUrl], 
[Project1].[IsActive] AS [IsActive], 
[Project1].[CreatedDate] AS [CreatedDate], 
[Project1].[CreatedBy] AS [CreatedBy], 
[Project1].[ModifiedDate] AS [ModifiedDate], 
[Project1].[ModifiedBy] AS [ModifiedBy], 
[Project1].[C1] AS [C1], 
[Project1].[ArtistLinkID] AS [ArtistLinkID], 
[Project1].[ArtistID1] AS [ArtistID1], 
[Project1].[LinkTitle] AS [LinkTitle], 
[Project1].[LinkUrl] AS [LinkUrl], 
[Project1].[CreatedDate1] AS [CreatedDate1], 
[Project1].[CreatedBy1] AS [CreatedBy1], 
[Project1].[ModifiedDate1] AS [ModifiedDate1], 
[Project1].[ModifiedBy1] AS [ModifiedBy1]
FROM ( SELECT 
    [Extent1].[ArtistID] AS [ArtistID], 
    [Extent1].[ArtistName] AS [ArtistName], 
    [Extent1].[ArtistNickname] AS [ArtistNickname], 
    [Extent1].[ArtistTitle] AS [ArtistTitle], 
    [Extent1].[ArtistDescription] AS [ArtistDescription], 
    [Extent1].[EnableSoundCloudDisplay] AS [EnableSoundCloudDisplay], 
    [Extent1].[SoundCloudUrl] AS [SoundCloudUrl], 
    [Extent1].[EnableYouTubeDisplay] AS [EnableYouTubeDisplay], 
    [Extent1].[YouTubeUrl] AS [YouTubeUrl], 
    [Extent1].[EnableFlickrDisplay] AS [EnableFlickrDisplay], 
    [Extent1].[FlickrSet] AS [FlickrSet], 
    [Extent1].[Notes] AS [Notes], 
    [Extent1].[IsFeatured] AS [IsFeatured], 
    [Extent1].[FeatureYouTubeUrl] AS [FeatureYouTubeUrl], 
    [Extent1].[IsActive] AS [IsActive], 
    [Extent1].[CreatedDate] AS [CreatedDate], 
    [Extent1].[CreatedBy] AS [CreatedBy], 
    [Extent1].[ModifiedDate] AS [ModifiedDate], 
    [Extent1].[ModifiedBy] AS [ModifiedBy], 
    [Extent2].[ArtistLinkID] AS [ArtistLinkID], 
    [Extent2].[ArtistID] AS [ArtistID1], 
    [Extent2].[LinkTitle] AS [LinkTitle], 
    [Extent2].[LinkUrl] AS [LinkUrl], 
    [Extent2].[CreatedDate] AS [CreatedDate1], 
    [Extent2].[CreatedBy] AS [CreatedBy1], 
    [Extent2].[ModifiedDate] AS [ModifiedDate1], 
    [Extent2].[ModifiedBy] AS [ModifiedBy1], 
    CASE WHEN ([Extent2].[ArtistLinkID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[Artists] AS [Extent1]
    LEFT OUTER JOIN [dbo].[ArtistLinks] AS [Extent2] ON [Extent1].[ArtistID] = [Extent2].[ArtistID]
    WHERE ([Extent1].[ArtistID] <> @p__linq__0) AND ([Extent1].[IsFeatured] = 1)
)  AS [Project1]
ORDER BY [Project1].[IsFeatured] DESC, [Project1].[CreatedDate] DESC, [Project1].[ArtistID] ASC, [Project1].[C1] ASC

-- p__linq__0: '2' (Type = Int32, IsNullable = false)



Really at a loss as to why I cannot get the artist object to deep load the links.

Thanks for your time.

Regards,
Steve