Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Appending more includes after initial fetch

$
0
0

I'm using EF with unit of work and repository patterns - a typical example is shown below.

        public static User Get(Guid userId)
        {
            Guard.ThrowDefault(userId, "userId");

            using (var uow = new Uow())
            {
                var r = new Repo<User>(uow.Context);

                var u = r.FindBy(x => x.UserId == userId)
                    .Include("LastVisited")
                    .Include("SavedDeals")
                    .SingleOrDefault();
            }
        }

In this example, I return a user and some of its related data through the use of .Include(). On some occassions, I then need to make decisions on the retrieved data and pull back more related data.

For instance, the user has a flag that indicates he/she is a member so I need to pull back MemberProfile. at the moment I have a user stored in the vaiable 'u', but how do I then pull back more without having to refetch the user?

if((UserRoles)u.UserRoleId == UserRoles.Member)

{

    u.Include("MoreData") <== its this kind of thing I want to do

}

Thanks.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>