【发布时间】:2013-05-20 11:03:03
【问题描述】:
我有一个带有几个可选参数的方法,然后使用 LINQ 查询更新数据库。我想更新记录,以便仅在方法中提供可选字段时才保存它们,否则这些值应保留其旧值。例如,
如果我向我的方法提供可选字段“errorMessage”,则数据库的相应记录字段应使用该值进行更新。如果 not 提供了方法值,那么我希望数据库的记录字段保留其现有内容(填充与否)。但是,当在我的查询中使用 ?? 运算符来应用此逻辑时,我收到以下错误;
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();
}
【问题讨论】:
-
Update是标准的 Linq2SQL 函数吗? -
@Magnus,是的,没有任何定制
-
你能指出我在 msdn 上的那个功能的文档吗?对于
Table类,它不会与other functions 一起出现 -
@Magnus,抱歉,我认为这是整个 EF 的标准(在 EF 仍然有点新)。
Update()方法是EntityFramework.Extensions的一部分,您可以在此处查看文档; efe.codeplex.com -
好的,您应该将您的问题直接提交给您正在使用的库的作者。看起来像一个错误。
标签: c# sql linq linq-to-sql