What's the function of Include () method in Entity Framework

I have two Permission and Access objects

Access.cs

public class Access
{
    public int Id { get; set; }

    public string Name { get; set; }

    public List<Permission> PermissionList { get; set; }
}

      

Permission.cs

public class Permission
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int ParentId { get; set; }

    [NotMapped]
    public bool HasChildren { get; set; }

    [NotMapped]
    public List<Permission> ChildPermissions { get; set; }
}

      

I also have a GenericRepository class for filtering records in my database.

GenericRepository.cs

 public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "", bool tracking = true)
        {
            IQueryable<TEntity> query = dbSet;


            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }
        /*...*/
        }

      

I am calling this method on the Access Service class

AccessService.cs

GenericRepository<Access> accessRepo = new GenericRepository<Access>();
List<Access> accessList = accessRepo.Get(d => d.Name == accessName, null, "PermissionList").OrderBy(d => d.Id).ToList();

      

This code filters the entry type "Access" and then uses the Include () method with the PermissionList parameter in the Generic Repository. What does the (PermissionList) method include? What does it do? PermissionList is an Access property and is of type Permission. But I could not reach the goal.

+3


source to share


1 answer


This is for loading uploaded objects.

See Entity Framework Loading Related Objects .

If you are not using Include () this query:

using (var context = new YourContext())
{
    var access = context.Access.Single(a => a.ID == 42);    
}

      

Would return an instance Access

with an empty property PermissionList

. Depending on how your context is set up, this collection will either remain empty (no lazy loading) or be lazy loaded as soon as you receive it ( foreach (var permission in access.PermissionList) { ... }

).

Now with Include()

:



using (var context = new YourContext())
{
    var access = context.Access.Include(a => a.PermissionList)
                               .Single(a => a.ID == 42);    
}

      

The request will be recorded as a connection, loading all the appropriate permissions for you.

The extension method Include()

also has a string overload that your repository code calls:

query = query.Include(includeProperty);

      

This results in a "PermissionList"

heavy load in your case and seems to support multiple Include()

using a comma separated list (for example "PermissionList,PermissionList.ChildPermissions"

).

+10


source







All Articles