1
votes

in ef core I have a contract entity which has a paymentRequirement entity. I want to bring back just the payment requirement entity given the contract's id.

public async Task<PaymentRequirement> GetPaymentRequirementByContractAsync(long Id) =>
            await context.Contracts.Include(p => p.PaymentRequirement).FirstOrDefaultAsync(p => p.Id == Id)?.PaymentRequirement

Severity Code Description Project File Line Suppression State Error CS1061 'Task' does not contain a definition for 'PaymentRequirement' and no accessible extension method 'PaymentRequirement' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?) Minerals C:\Users\c-bdelling\source\repos\Minerals\Minerals\Repositories\PaymentRequirementRepository.cs 15 Active

here is the contract

 public class Contract : AuditedEntity
    {
        public long Id { get; set; }       
        public long? PaymentRequirementId { get; set; }
        public PaymentRequirement PaymentRequirement { get; set; }

    }

here is paymentrequirment

 public class PaymentRequirement
    {
        public long Id { get; set; }
        public decimal? FloorPrice { get; set; }
    }
1
why are you using ? ?vivek nuna
in case contract does not exist like the id was 0Bryan Dellinger

1 Answers

2
votes

The result of the entire expression is being awaited, and PaymentRequirement is not awaitable:

context.Contracts
    .Include(p => p.PaymentRequirement)
    .FirstOrDefaultAsync(p => p.Id == Id)?
    .PaymentRequirement

You really need to be awaiting the result of FirstOrDefaultAsync, and you can do this by introducing parenthesis:

(await context.Contracts
    .Include(p => p.PaymentRequirement)
    .FirstOrDefaultAsync(p => p.Id == Id))?
    .PaymentRequirement

Having said this, there should be a cleaner way of doing this, assuming PaymentRequirements is a DbSet on your context:

await context.PaymentRequirements
    .FirstOrDefaultAsync(p => context.Contracts
        .Any(c => c.Id == Id && c.PaymentRequirement == p));

That way, only the PaymentRequirement and not the Contract will be returned from the database.