使用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,然後用它作爲服務器端更新的數據源......但是,當清楚這是一個很大的努力預期的場景...對嗎?
謝謝!
建議:擺脫'UpdateDealer'中的try/catch,並將返回類型更改爲'void'。您忽略了返回值,並且您也忽略了任何異常。 – 2009-08-29 21:40:47