首页 > 解决方案 > 未映射的属性未获取外键值

问题描述

在我的项目中,我使用的是 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;
    }
}

标签: entity-framework-core

解决方案


推荐阅读