在ef核心中,我有一个合同实体,其中有一个paymentRequirement实体。我只想带给定合同ID的付款要求实体。
public async Task<PaymentRequirement> GetPaymentRequirementByContractAsync(long Id) =>
await context.Contracts.Include(p => p.PaymentRequirement).FirstOrDefaultAsync(p => p.Id == Id)?.PaymentRequirement
严重性代码说明项目文件行抑制状态错误CS1061'任务'不包含'PaymentRequirement'的定义,并且找不到可以接受的扩展方法'PaymentRequirement'接受类型为'Task'的第一个参数(您是否缺少using指令或程序集引用?)Minerals C:\ Users \ c-bdelling \ source \ repos \ Minerals \ Minerals \ Repositories \ PaymentRequirementRepository.cs 15有效
这是合同
public class Contract : AuditedEntity
{
public long Id { get; set; }
public long? PaymentRequirementId { get; set; }
public PaymentRequirement PaymentRequirement { get; set; }
}
这是付款要求
public class PaymentRequirement
{
public long Id { get; set; }
public decimal? FloorPrice { get; set; }
}
整个表达式的结果正在等待,PaymentRequirement
不是等待的:
context.Contracts
.Include(p => p.PaymentRequirement)
.FirstOrDefaultAsync(p => p.Id == Id)?
.PaymentRequirement
您确实需要等待的结果FirstOrDefaultAsync
,可以通过引入括号来做到这一点:
(await context.Contracts
.Include(p => p.PaymentRequirement)
.FirstOrDefaultAsync(p => p.Id == Id))?
.PaymentRequirement
话虽如此,应该有这样做的更清洁的方式,假设PaymentRequirements
是DbSet
在你的背景:
await context.PaymentRequirements
.FirstOrDefaultAsync(p => context.Contracts
.Any(c => c.Id == Id && c.PaymentRequirement == p));
这样,只会从数据库中返回PaymentRequirement
而不是Contract
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句