1

我有一个带有几个可选参数的方法,然后使用 LINQ 查询更新数据库。我想更新记录,以便只有在方法中提供可选字段时才保存它们,否则这些值应该保留它们的旧值。例如,

如果我向我的方法提供可选字段“errorMessage”,则数据库的相应记录字段应使用该值进行更新。如果提供方法值,那么我希望数据库的记录字段保留其现有内容(填充与否)。但是,??在查询中使用运算符应用此逻辑时,出现以下错误;

The parameterized query '(@p__linq__0 int,@p__update__0 nvarchar(198),@p__update__1 nvarc' expects the parameter '@p__update__1', which was not supplied.

我知道足够多的 LINQ 知道这基本上意味着将 LINQ 转换为 SQL 时遇到问题。同样,我可以通过首先加载项目、将方法值应用于检索到的对象然后保存它们来解决这个问题,但这会将一步操作变成三步操作。我希望有一个我不知道的简洁优雅的解决方案。有任何想法吗?

我的代码;

public void UpdateInvoiceExport(int quickBooksExportID, bool successful, string failureMessage = null,
                                int? failureRequestID = null, int? failureStatusCode = null,
                                string failureSeverity = null)
{
    // only update certain details if the export was successful
    DateTime? exportDateTime = null;
    if (successful)
    {
        exportDateTime = DateTime.Now;
    }

    // update the export item
    _context.QuickBooksExports.Update(item => item.QuickBooksExportID == quickBooksExportID,
                                        qb => new QuickBooksExport
                                        {
                                            DateTimeExported = exportDateTime,
                                         // the addition of the ?? operators here seems to cause the error
                                            LastFailureMessage = failureMessage ?? qb.LastFailureMessage,
                                            FailureRequestID = failureRequestID ?? qb.FailureRequestID,
                                            FailureStatusCode = failureStatusCode ?? qb.FailureStatusCode,
                                            FailureSeverity = failureSeverity ?? qb.FailureSeverity
                                        });
    _context.SaveChanges();
}
4

1 回答 1

1

我以前遇到过这个问题,我必须将对象拉出,先构建它然后将其传递。这可以防止 ?? 运算符从位于 Linq 语句中。

public void UpdateInvoiceExport(int quickBooksExportID,
                                bool successful,
                                string failureMessage = null,
                                int? failureRequestID = null,
                                int? failureStatusCode = null,
                                string failureSeverity = null)
{
    // only update certain details if the export was successful
    DateTime? exportDateTime = null;
    if (successful)
    {
        exportDateTime = DateTime.Now;
    }

    // update the export item
    var quickBooksExport = new QuickBooksExport;
                            {
                                DateTimeExported = exportDateTime
                            };
    quickBooksExport.LastFailureMessage = failureMessage ?? qb.LastFailureMessage;
    quickBooksExport.FailureRequestID = failureRequestID ?? qb.FailureRequestID;
    quickBooksExport.FailureStatusCode = failureStatusCode ?? qb.FailureStatusCode;
    quickBooksExport.FailureSeverity = failureSeverity ?? qb.FailureSeverity;
    _context.QuickBooksExports.Update(item => item.QuickBooksExportID == quickBooksExportID,
                                        qb => quickBooksExport);
    _context.SaveChanges();
}
于 2013-07-12T14:59:06.970 回答