Milo
Milo

Reputation: 3443

EF Core Accessing Nested-nested Entity

I have an entity CallTrackerLog which has many Clients which have a one-many Advices. I am trying to HttpPost a create for the advice:

    [HttpPost("{callTrackerId}/{clientId}/advice")]
    public IActionResult CreateCTClientAdvice(int callTrackerId, int clientId,
        [FromBody] CallTrackerClientAdvice newAdvice)
    {
        if (newAdvice == null)
            return BadRequest();

        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        var ctFromStore = _context.CallTrackers
            .Include(log => log.CallTrackerClients)
                .ThenInclude(log => log.CallTrackerClientAdvice)
            .FirstOrDefault(ct => ct.CallTrackerId == callTrackerId);

        var ctAdviceFromStore ctFromStore.CallTrackerClients.CallTrackerClientAdvice
            .FirstOrDefault(c => c.CallTrackerClientId == clientId);

        // ... add to db

        return Ok();
    }

The problem is that I cannot access the CallTrackerClientAdvice with the .FirstOrDefault(ct => ct.CallTrackerClientId == clientId) - it gives me a red underline even though I thought I loaded it above.

The error:

enter image description here

How come I am unable to access the CallTrackerClientAdvice?

Upvotes: 0

Views: 297

Answers (1)

Ricardo Peres
Ricardo Peres

Reputation: 14525

I suspect that what you want is:

var ctAdviceFromStore = ctFromStore.CallTrackerClients
        .FirstOrDefault(c => c.CallTrackerClientId == clientId)?.CallTrackerClientAdvice;

Upvotes: 1

Related Questions