none
EF Iclude RRS feed

  • Question

  • Hi

    in my C# project library, i have this Entity:

    public class Race

    {

    ...

       public List<CategoryRace> CategoryRaces { get; set; }

    }

    and

    public class CategoryRace

    {

    ...

       public List<Category> Categories { get; set; }

    }

    How can I include Multiple Levels for load data also in all these navigation properties?

    I need to have Race.CatRaces.First().Categories.First().Name

    What i have to write in the Include method for load "CategoryRaces" and "Category" navigation prop?

    using (var context = new EntityContext())
    {
        var test = context.Race
                .Include("??") //??????
                .ToList();
    }

    THANKS


    • Edited by Pengo11 Thursday, March 7, 2019 3:06 PM
    • Moved by CoolDadTx Friday, March 8, 2019 3:06 PM EF related
    Thursday, March 7, 2019 3:06 PM

Answers

  • Hello,

    See if this makes sense, using NorthWind database as a model first set lazy loading off then to control which navigations to include use the following syntax to get in this case contact information for customers which is strong typed while using .Include("xxxxx") means if the model changes this can break your code as the item in .Include is only a string.

    This is strong typed e.g. ContactType is a navigation property off Customers.

    using (var context = new northWindContext())
    {
        context.Configuration.LazyLoadingEnabled = false;
    
        var allCustomers = context.Customers
            .Include(customer => customer.Contact)
            .Include(customer => customer.ContactType) 
            .Include(customer => customer.Contact.ContactContactDevices)
            .ToList();
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Pengo11 Friday, March 8, 2019 3:07 PM
    Friday, March 8, 2019 2:20 AM

All replies

  • using (var context = new EntityContext())
    {
        var test = (context.Race
              .Include("CategoryRaces")
              .Include("Category")).ToList();
    }

    }

    Thursday, March 7, 2019 8:42 PM
  • Hello,

    See if this makes sense, using NorthWind database as a model first set lazy loading off then to control which navigations to include use the following syntax to get in this case contact information for customers which is strong typed while using .Include("xxxxx") means if the model changes this can break your code as the item in .Include is only a string.

    This is strong typed e.g. ContactType is a navigation property off Customers.

    using (var context = new northWindContext())
    {
        context.Configuration.LazyLoadingEnabled = false;
    
        var allCustomers = context.Customers
            .Include(customer => customer.Contact)
            .Include(customer => customer.ContactType) 
            .Include(customer => customer.Contact.ContactContactDevices)
            .ToList();
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Pengo11 Friday, March 8, 2019 3:07 PM
    Friday, March 8, 2019 2:20 AM