如何在客户端扩展WCF数据服务上下文类?

时间:2013-04-10 09:25:13

标签: wcf wcf-data-services odata

我正在使用以下代码在客户端上使用FullName扩展我的Service-Context:

public partial class Customer {
    public string FullName
    {
        get
        {
            return string.Concat(LastName, (FirstName == "" ? "" : ", "), FirstName);
        }
    }
}

这很好,直到我需要使用AddOBject方法向Context添加新对象。它会引发异常。当我删除FullName扩展时,AddObject方法将新Object保存到数据库。 扩展我的Context并仍然使其更新和可插入的最佳方法是什么?

编辑: DataServiceRequest异常:

System.Data.Services.Client.DataServiceRequestException was not handled by user code.
  HResult=-2146233079
  Message=Fehler beim Verarbeiten dieser Anforderung.
  Source=Microsoft.Data.Services.Client.WindowsStore
  StackTrace:
       at System.Data.Services.Client.SaveResult.HandleResponse()
       at System.Data.Services.Client.BaseSaveResult.EndRequest()
       at System.Data.Services.Client.DataServiceContext.EndSaveChanges(IAsyncResult asyncResult)
       at Pointsale.Client.Service.PointsaleEntities.<SaveChanges>b__0(IAsyncResult asResult) in c:\Users\Jan\Desktop\pointsale_worksapce\pointsale.client\Helper\TenantHelper.cs:line 98
       at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
  InnerException: System.Data.Services.Client.DataServiceClientException
       HResult=-2146233079
       Message=<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code /><m:message xml:lang="de-DE">Error occurred while processing this request.</m:message></m:error>
       StatusCode=400
       InnerException: 

并使用SaveChanges方法将其设置为异步:

public async Task<DataServiceResponse> SaveChanges()
{
    var queryTask = Task.Factory.FromAsync(this.BeginSaveChanges(null, null), asResult =>
    {

        var result = this.EndSaveChanges(asResult);
        return result;

    });
    return await queryTask;
}

在EndSaveChanges上发生错误。

0 个答案:

没有答案