简体   繁体   中英

.NET Core Web Api Loading Nested Entities

I have a db model that looks like:

    public CallTrackers()
    {
        CallTrackerCallerTelns = new HashSet<CallTrackerCallerTelns>();
        CallTrackerClients = new HashSet<CallTrackerClients>();
        CallTrackerFlwups = new HashSet<CallTrackerFlwups>();
        CallTrackerHistory = new HashSet<CallTrackerHistory>();
    }

With my GetAll() I am loading everything fine except for CallTrackerClients has 3 nested objects that I can't retrieve:

    public CallTrackerClients()
    {
        CallTrackerClientAdvice = new HashSet<CallTrackerClientAdvice>();
        CallTrackerClientOffences = new HashSet<CallTrackerClientOffences>();
        CallTrackerClientSureties = new HashSet<CallTrackerClientSureties>();
    }

I am trying:

    [HttpGet]
    public IEnumerable<CallTrackers> GetAll()
    {
        return _context.CallTrackers
            .Include(log => log.CallTrackerClients)
                .ThenInclude(c => c.CallTrackerClientAdvice)
            .Include(log => log.CallTrackerCallerTelns)
            .Include(log => log.CallTrackerFlwups)
            .Include(log => log.CallTrackerHistory)
            .ToList();
    }

The above works but I need to get the Sureties and Offences as well. When I try .ThenInclude(c => c.CallTrackerClientOffences) I get some 'does not contain definition' error.

Any ideas how to get the two remaining collections that are part of CallTrackerClients?

You always have to start from the parent entity.

    return _context.CallTrackers
        .Include(log => log.CallTrackerClients)
            .ThenInclude(c => c.CallTrackerClientAdvice)
        // this
        .Include(log => log.CallTrackerClients)
            .ThenInclude(c => c.CallTrackerClientOffences)
        // and this
        .Include(log => log.CallTrackerClients)
            .ThenInclude(c => c.CallTrackerClientSureties)
        .Include(log => log.CallTrackerCallerTelns)
        .Include(log => log.CallTrackerFlwups)
        .Include(log => log.CallTrackerHistory)
        .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM