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

Nested Entities Reference - Confused

$
0
0

I have been working my way through my first stand alone application.  It is broadly based on the Contosso University tutorial and I am working in Asp.Net Core.

So far, I have been having pretty good results and think I have a good grasp on the basics...it took a while...but I got there.

However I am now moving a layer out from the code I knew would mimic the tutorial to a large extent.  I have a bit of confusion on this aspect.

Within the SitesController I have the following:

 public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var site = await _context.Sites
                .Include(w=>w.SiteType)
                    .ThenInclude(w=>w.HealthGroup)
                .Include(w => w.WaterBodys)
                    .ThenInclude(w => w.Equipment)
                    .AsNoTracking()
                .SingleOrDefaultAsync(m => m.SiteID == id);
            if (site == null)
            {
                return NotFound();
            }

            return View(site);
        }

This works fine.  As can be seen I have a table HealthGroup.  Health Group includes Group id.

In relational model I have several other tables that use GroupID to return more information.  For example First Aid Kit requirement.

My data model in rough is:

Health Group - data specific to the group

HealthGrouptoAid - linking table.  Different groups have different first aid kit requirements for ease say group 1 has 1,2,3; group 2 might have 3,5,8 etc

HealthFirstAid - contains the requirements e.g. stretcher, pillow and blankets etc

I feel like I need to write something like:

            var site = await _context.Sites
                .Include(w=>w.SiteType)
                    .ThenInclude(w=>w.HealthGroup)
                          .(Then)Include(w=>w.HealthGroupToKit) //assuming this picks up the GroupID from Health Group
                              .(Then)Include(w=>w.HealthFirstAidKit) //assuming this picks up from HealthGroupToKit
                .Include(w => w.WaterBodys)
                    .ThenInclude(w => w.Equipment)
                    .AsNoTracking()
                .SingleOrDefaultAsync(m => m.SiteID == id);

However this doesn't work and intellisense is telling me it is wrong.  I am not sure what to Google to find an example of how to do this.  I am hoping someone here may be able to assist me in improving my understanding and enabling me to move past this roadblock.


Viewing all articles
Browse latest Browse all 1698

Trending Articles