我有WCF数据服务应用程序。后端数据库是Ingress,它使用nHibernate。
我正在尝试从其中一个表中获取和更新记录。其结构如下:
前三个突出显示的列构成主键。
在我的客户端应用程序中,我正在查询如下数据:
private void LoadItemDetails(string itemCode, int commentDate)
{
using(var dc = new MyDataContext(new Uri("http://myservice.myserver.com/Dev/com.OData/IngressData.svc")))
{
var itemDetails = dc
.ItemLineDetails
.Where(d => d.ItemCode.Equals(itemCode) &&
ld.CommentDate == commentDate);
ObsItemLineDetails = new ObservableCollection<ItemLineDets>();
}
}
然后我更新并保存数据,如下所示:
private void SaveItemDetails()
{
using(var dc = new MyDataContext(new Uri("http://myservice.myserver.com/Dev/com.OData/IngressData.svc")))
{
foreach(var itemLine in ObsItemLineDetails )
{
var today = DateTime.Today.AddSeconds(-1);
var newCommentDate = (Int32)(today.Subtract(new DateTime(1970, 1, 1,0,0,0))).TotalSeconds;
var itemDetail = dc.ItemLineDetails.Where(d => d.ItemCode.Equals(itemLine.ItemCode) &&
d.LineId == itemLine.LineId &&
d.SearchDate == itemLine.SearchDate).FirstOrDefault();
if (itemDetail != null)
{
itemDetail.CommentDate = newCommentDate;
itemDetail.Comments = GetAutoComment();
dc.UpdateObject(itemDetail);
}
}
dc.SaveChanges();
}
}
在调用SaveChanges
时,它会在最后一行引发异常。以下是IIS服务器日志条目。
15:52:42.7823 Trace 20 com.OData.DataServiceBase`1 Received: http://myservice.myserver.com/Dev/com.OData/IngressData.svc/ItemLineDetails(ItemCode='001/152/0101',LineId=3,SearchDate=11070945856)
15:52:42.7823 Error 20 com.OData.DataServiceBase`1 Bad Request - Error in query syntax.
at System.Data.Services.RequestUriProcessor.ParsePath(Uri absoluteRequestUri, IDataService service)
at System.Data.Services.RequestUriProcessor.ProcessRequestUri(Uri absoluteRequestUri, IDataService service, Boolean internalQuery)
at System.Data.Services.DataService`1.HandleRequest() at System.Data.Services.RequestUriProcessor.ParsePath(Uri absoluteRequestUri, IDataService service)
at System.Data.Services.RequestUriProcessor.ProcessRequestUri(Uri absoluteRequestUri, IDataService service, Boolean internalQuery)
at System.Data.Services.DataService`1.HandleRequest()
我认为它可能是我的ITEM_CODE字段中的正斜杠导致了这个问题。
如果是这种情况,是否有任何解决方法。
答案 0 :(得分:0)
尝试在函数中使用DataContext而不是全局上下文对象 如下所示
private void SaveItemDetails()
{
using(context dc = new context())
{
foreach(var itemLine in ObsItemLineDetails )
{
var today = DateTime.Today.AddSeconds(-1);
var newCommentDate = (Int32)(today.Subtract(new DateTime(1970, 1, 1,0,0,0))).TotalSeconds;
var itemDetail = dc.ItemLineDetails.Where(d => d.ItemCode.Equals(itemLine.ItemCode) &&
d.LineId == itemLine.LineId &&
d.SearchDate == itemLine.SearchDate).FirstOrDefault();
if (itemDetail != null)
{
itemDetail.CommentDate = newCommentDate;
itemDetail.Comments = GetAutoComment();
dc.UpdateObject(itemDetail);
}
}
dc.SaveChanges();
}
}