C# “创建控制器”;对象引用未设置为对象的实例;

C# “创建控制器”;对象引用未设置为对象的实例;,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,我在ASP Net核心应用程序上有一个创建控制器,它创建一个新的FinancePaymentReceive 控制器 [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("FinanceInvoiceID,Payment_Reference_Number,FinancePaymentTypeID,Payment_Amount,Payment_Date")] Financ

我在ASP Net核心应用程序上有一个创建控制器,它创建一个新的
FinancePaymentReceive

控制器

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("FinanceInvoiceID,Payment_Reference_Number,FinancePaymentTypeID,Payment_Amount,Payment_Date")]  FinancePaymentReceipt financePaymentReceipt)
{
    var invoice = await _context.FinanceInvoices
                                .Include(fi=>fi.FinanceDailyClaim)
                                            .ThenInclude(fdc=>fdc.Department)
                                               .Where(ss=>ss.FinanceInvoiceID==financePaymentReceipt.FinanceInvoiceID)
                                                .SingleOrDefaultAsync();

   if (invoice == null)
   {
       return NotFound();
   }

   if (ModelState.IsValid)
    {
       financePaymentReceipt.PaymentReceiptNumber = await _numberSequence.GetNumberSequence("RECPT", invoice.FinanceDailyClaim.Department.Department_Code, 'C');

        _context.Add(financePaymentReceipt);

        await _context.AddAsync(new FinanceAccountSatement
        {
           FinancePaymentReceiptID = financePaymentReceipt.FinancePaymentReceiptID,
           Activity_Date = financePaymentReceipt.Date_Captured
         });

         await _context.SaveChangesAsync();

         return RedirectToAction(nameof(Index));
}
从上面的
financepaymentreceive
模型中,我有一个属性
Description
,它只有
get
accesor,并从
FinanceInvoice
模型中检索发票号

但问题是,当我创建它时,在将模型保存到数据库之前尝试获取
描述
,事实上,如果我在
创建控制器
的开始处放置断点,它甚至不会命中

当我单击“创建”按钮时,我只得到错误
对象引用未设置为对象的实例
`


有办法解决这个问题吗?

一种解决方案是,如果尚未设置
财务发票
,则只需为
说明
返回
null
。大概是这样的:

 public string Description
 {
    get
    {
            return FinanceInvoice == null
                       ? null
                       : $"Payment for {FinanceInvoice.Invoice_Number} Thank You";
    }
  }

我正在使用C#在那里。

尝试将
[NotMapped]
添加到不在数据库中的属性。
 public string Description
 {
    get
    {
            return FinanceInvoice == null
                       ? null
                       : $"Payment for {FinanceInvoice.Invoice_Number} Thank You";
    }
  }