2009-08-29 54 views
0

使用Linq2Sql作爲Wcf服務的驅動程序。讓我們去自下而上....通過Wcf序列化Linq2Sql - 錯誤還是誤解?

唐氏在底部,我們有打LINQ2SQL的方法...

public virtual void UpdateCmsDealer(CmsDealer currentCmsDealer) 
{ 
    this.Context.CmsDealers.Attach(currentCmsDealer, 
      this.ChangeSet.GetOriginal(currentCmsDealer)); 
} 

這被使用我的WCF服務這樣...

public bool UpdateDealer(CmsDealer dealer) 
{ 
    try 
    { 
     domainservice.UpdateCmsDealer(dealer); 
     return true; 
    } 
    catch 
    { 
     return false; 
    } 
} 

而且從我的WPF客戶端代碼這樣(僞下同)叫...

[...pull the coreDealer object from Wcf, it is a CmsDealer...] 
[...update the coreDealer object with new data, not touchign the relation fields...] 
try 
{ 
    contextCore.UpdateDealer(coreDealer); 
} 
catch (Exception ex) 
{ 
    [...handle the error...] 
} 

現在,CmsDealer類型都有> 1 < foriegn密鑰關係,它使用「StateId」字段鏈接到CmsItemStates表。所以是的,在上面的coreDealer.StateId是填充的,我可以訪問coreDealer.CmsItemState.Title上的數據顯示我適當的狀態的瓷磚。現在

,這裏是東西...如果你註釋掉...

domainservice.UpdateCmsDealer(dealer); 

在WCF服務仍與下面的異常,這表明對我的炸彈,這是不真的是Linq2Sql問題,而不是Linq2Sql而不是Wcf問題。

"System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException was unhandled by user code 
    Message="Operation is not valid due to the current state of the object." 

InnerException爲NULL。最終的結果是,當它發生錯誤處理程序(Catch ex bloc)時,異常消息會報告解串器。當我可以搶奪一個調試時,引發錯誤的實際代碼是來自Linq2Sql構建的CmsDealer模型代碼的snippit。

[Column(Storage="_StateId", DbType="UniqueIdentifier NOT NULL")] 
public System.Guid StateId 
{ 
    get 
    { 
     return this._StateId; 
    } 
    set 
    { 
     if ((this._StateId != value)) 
     { 
      if (this._CmsItemState.HasLoadedOrAssignedValue) 
      { 
       throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); 
      } 
      this.OnStateIdChanging(value); 
      this.SendPropertyChanging(); 
      this._StateId = value; 
      this.SendPropertyChanged("StateId"); 
      this.OnStateIdChanged(); 
     } 
    } 
} 

簡而言之,它會出現一些東西正在發生「封面」這是很好,但文件不存在。地獄googleing爲「ForeignKeyReferenceAlreadyHasValueException」變成幾乎沒有:)

我寧願繼續使用直接通過Wcf Linq2Sql對象。如果需要的話,我可以創建一個沒有任何關聯的平面代理類,將它發佈到Wcf,然後用它作爲服務器端更新的數據源......但是,當清楚這是一個很大的努力預期的場景...對嗎?

謝謝!

+0

建議:擺脫'UpdateDealer'中的try/catch,並將返回類型更改爲'void'。您忽略了返回值,並且您也忽略了任何異常。 – 2009-08-29 21:40:47

回答

1

該錯誤可能是由於在最初設置fk值後發生了某些更改 - 您確定在某處可能沒有初始設置該值的某個自定義初始化代碼?

你可以斷點集(它拋出的地方),並在每次設置時跳出(如果需要的話跳過異常),這應該有希望指向正確的方向。

+0

感謝您的幫助。在Wcf序列化器/解串器不喜歡用關係數據發送(clinet-> server)Linq對象的問題上,我不得不使用代理對象。 – 2009-08-30 07:42:52

+0

這種情況一定會起作用(或者至少我不記得有問題了) - 看看哪些代碼設置/改變FK值會很有趣,但是如果你的工作方式不同,那麼這是主要的問題: - ) – 2009-08-30 08:07:17

+0

是的,我從來沒有弄明白,我將再次在未來中再次訪問! – 2009-09-05 23:01:22

2

默認序列化程序將首先設置狀態,它將設置StateId。之後,它將嘗試設置序列化的StateId,然後拋出異常。

問題是你沒有指定你想讓你的類使用DataContract屬性進行修飾。

轉到您的LinqToSqlGenerator的屬性和設置序列化模式單向

這將導致該工具將DataMember屬性添加到所需的屬性,你會看到STATEID不會一個DataMember,因爲它將在反序列化時設置State屬性時自動設置。