在使用存储过程更新 table 后,Dapper 停止拉入 table

Dapper stopped pulling joined table after updating the table with a stored procedure

因为我遇到了这个错误

InnerException = {"Cannot insert explicit value for identity column in table 'Loan' when IDENTITY_INSERT is set to OFF."}

所以决定改成这个存储过程

this._context.Loan.FromSqlInterpolated($"Exec CreateNewLoan @CurrencyCode = {loan.CurrencyCode}, @OperativeAcctNo = {loan.OperativeAcctNo}, @AmountWrittenOff={loan.AmountWrittenOff}, @OriginalLoanAmount={loan.OriginalLoanAmount}, @LoanTrfDate={loan.LoanTrfDate}, @InterestRate={loan.InterestRate}, @LoanAccountNo={loan.LoanAccountNo}, @DRCR={loan.DRCR}, @CustId={loan.CustId} ");

插入记录成功,插入页面后会重定向到索引页面,并调用另一个方法拉取列表并用这个dapper query显示

public async Task<IEnumerable<Loan>> GetAll(bool includeDeleted, bool showUnapprovedOnly)
{
    IEnumerable<Loan> loans = null;

    try
    {
        using (var conn = new SqlConnection(connectionstring))
        {
            await conn.OpenAsync();
            //
            string sql = "Select l.*, c.FirstName_CompanyName from dbo.Loan l left join Customer c on  l.CustId=c.CustId";
            if (!includeDeleted)
            {
                sql += " and l.Deleted = 0";
            }
            if (showUnapprovedOnly)
            {
                sql += " and l.Approved = 0";
            }

            loans = await conn.QueryAsync<Loan>(sql);
        }
    }
    catch (Exception)
    {

        throw;
    }
    return loans;
}

贷款与客户之间存在关系table。客户 Id (CustId) 也随存储过程一起插入。我注意到没有显示新插入记录的客户名称。以前的正在显示。不知道为什么新唱片不拉客table\

   public class Loan
    {
        public int LoanId { get; set; }

        [Display(Name ="Customer Name")]
        public long CustId { get; set; }

        [NotMapped]
        [Display(Name ="Customer")]
        public string FirstName_CompanyName { get; set; }

        [Display(Name = "Currency Code")]
        [StringLength(10)]
        [Required]
        public string CurrencyCode { get; set; }

        [Display(Name = "Loan Account No.")]
        [Key]
        public string LoanAccountNo { get; set; }

        [Display(Name = "Branch Code")]
        [StringLength(5)]
        public string BranchCode { get; set; }

        [Display(Name = "Amount Written Off")]
        [Required]
        public double? AmountWrittenOff { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Loan Tranfer Date")]
        [Required]
        public DateTime? LoanTrfDate { get; set; }

        [Display(Name = "Interest Rate")]
        [Required]
        public double? InterestRate { get; set; }

        [ForeignKey("CustId")]
        public virtual Customer Customer { get; set; }

    }

看起来 FirstName_CompanyName 没有映射到任何 class 属性。 此映射更改应该可以解决问题。

var sql = "Select l.*, c.CustId, c.FirstName_CompanyName as Name from dbo.Loan l left join Customer c on  l.CustId=c.CustId";
var loans = connection.Query<Loan, Customer, Loan>(
        sql,
        (loan, customer) =>
        {
            loan.CustomerName = customer?.Name;
            return loan;
        },
        splitOn: "CustId")
    .Distinct()
    .ToList();