2012-07-24 54 views
0

問題:在entity_A的插入操作期間,我想更新特定entity_B上的屬性並更新更改。我這樣做如下:RIA服務 - 在插入期間更新

public void InsertEntity_A(Entity_A entity_A) 
{ 
    // fetch entity_B and change a property 
    Entity_B entity_B = ObjectContext.Entity_B.SingleOrDefault(...); 
    entity_B.Counter++; 
    this.UpdateEntity_B(entity_B); 

    // continue normal processing for entity_A 
    if((entity_A.EntityState != ...) 
    ... 
    ... 
} 

「UpdateEntity_B(...)」會生成一個錯誤,指出entity_B不在當前ChangeSet中。我怎樣才能做到這一點?

TIA 海科

+0

是什麼'UpdateEntity()'什麼樣子的?它使用了不同的ObjectContext嗎?它是否從'this.ChangeSet'中拉出任何東西? – 2012-07-25 15:24:41

+0

UpdateEntity()是RIA Services生成的代碼,它看起來像'this.ObjectContext.Entity_B.AttachAsModified(currentEntity_B,this.ChangeSet.GetOriginal(currentEntity_B));'由於實體當前不在ChangeSet中,錯誤會彈出向上。 – okieh 2012-08-03 08:11:29

回答

0

如果發現,使用

ObjectContext.Entity_B.ApplyCurrentValues(entity_B); 

,而不是UpdateEntity()的伎倆,顯然可以節省entity_B到數據庫中。任何人都可以證實這是正確的做法嗎?

問候 海科