entity-framework-core - 未映射的属性未获取外键值
问题描述
在我的项目中,我使用的是 Entity Framework Core。我有以下模型,并且有一个NotMapped Property 可以计算其他外部属性值。
public class Member
{
public Member()
{
Id = 0;
IsActive = true;
BillDetails = new List<BillDetail>();
Payments = new List<Payment>();
BankPayments = new List<BankPayment>();
DueReconciles = new List<DueReconcile>();
}
public int Id { get; set; }
public string Code { get; set; }
public string Title { get; set; }
public string Gender { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string SpouseName { get; set; }
public string NationalId { get; set; }
public decimal OpeningBalance { get; set; }
public Organization Organization { get; set; }
public IList<BillDetail> BillDetails { get; set; }
public IList<Payment> Payments { get; set; }
public IList<BankPayment> BankPayments { get; set; }
public IList<DueReconcile> DueReconciles { get; set; }
[NotMapped]
public decimal CurrentBalance
{
get
{
var totalBill = BillDetails.Where(x => x.IsActive == true).Sum(x => x.Amount);
var totalPayment = Payments.Where(x => x.IsActive == true).Sum(x => x.Amount);
var totalReconcile = DueReconciles.Where(x => x.IsActive == true).Sum(x => x.ReconcileAmount);
return Math.Round(((OpeningBalance + totalBill) - (totalPayment + totalReconcile)), 2);
}
}
}
在 NotMapped 属性CurrentBalance属性中返回无效金额。BillDetail属性具有值,但在 CurrentBalance 属性中,totalBill为 0。
谁能给我建议。
这是我的服务代码:
public MemberListResult Lists(int? orgId)
{
try
{
var lists = _memberRepository.Query().AsNoTracking().Where(x => x.OrgId == orgId).ToList();
if (lists == null || lists.Count <= 0)
{
return new MemberListResult(TaskResult.StatusCodes.ERROR)
{
Message = "Member lists are not found.",
Status = TaskResult.StatusCodes.ERROR,
ErrorNumber = 1
};
}
return new MemberListResult(TaskResult.StatusCodes.SUCCESS)
{
Members = lists
};
}
catch (Exception ex)
{
throw ex;
}
}
解决方案
推荐阅读
- wordpress - 如何使用 Wordpress 文件系统方法读取 php 输入流?
- mongodb - 如何从集合中的所有文档中删除特定的子文档?
- html - css不适用于div
- dotnetnuke - 将 Google Tag Manager 与 DNN 结合使用时,无法使用结构数据测试工具检测架构代码
- ios - 框架更改时不重绘 UIView 背景颜色(在动态扩展的 TableViewCell 中)
- android - 如何使用共享首选项第一次而不是第二次打开视图寻呼机?
- symfony - 使用监听器以表单形式注入的 Symfony 更改对象
- c# - 如何在 Automation Anywhere 等 RPA 工具中模拟击键
- java - 如何在单击按钮时更新回收站视图中的文本视图?
- javascript - 为什么这段代码没有返回正确的结果(8.675)