2011-11-25 65 views
0

我在那裏,我讀了DB對象的方法,例如:爲什麼我的對象沒有在linq中更新?

public Object getProduct(int categoryId, int productId) 
{ 
     DataClassesDataContext db = new DataClassesDataContext(Settings.getDefaultConnectionStringName()); 

     switch (categoryId) 
     { 
      case CCategorii.CARTI_ID: 
       { 
        IEnumerable<Carti> product = (from c in db.Cartis 
                where c.Carti_id == productId 
                && c.Vizibil == true 
                select c); 

        if (product.Count() != 0) 
         return product.First(); 

        break; 
       } 
      //so on 
     } 
} 

現在我有另一種方法,我做了更新:​​

public void updateProduct() 
{ 
    Object productToBeUpdated = getProduct(1,1); 
    DataClassesDataContext db = new DataClassesDataContext(Settings.getDefaultConnectionStringName()); 
    //update some properties of the product 
    productToBeUpdated.setQuantity(productToBeUpdated.getQuantity()+1); 
    db.submitChanges(); 
} 

好,該產品succcesfully從以前的閱讀方法,但沒有對數據庫進行更改。

我認爲原因是我在兩個不同的DataContext中執行READ-UPDATE ... 如果這是原因,您如何威脅這種情況?

哦,是的,我可以讀取產品和更新在同一個方法,但這意味着複製我用於閱讀和添加到它更新的東西...我想避免這種方法。

+0

創建的DB一個實例並更新你......正在編輯一個實例並保存另一個實例。 –

回答

1

你是使用您的DataContext的兩個不同的實例。

當實現一個web應用程序時,最好的選擇通常是將DataContext的生命週期與一個http請求的生命週期對齊。你使用的時間太短。

另一種選擇是將對象附加到寫的DataContext:

db.Cartis.Attach(yourReadObject); 
updateProperties(yourReadObject); 
db.submitChanges(); 

編輯

好吧,你必須首先從其他方面分離的對象。有關如何操作,請參閱this article

但我真的會建議使用單個DataContext對象,並將生命期延長到httprequest範圍。

這可以通過一個像autofac這樣的ioc容器來實現。

+0

似乎不支持:'嘗試附加或添加一個不是新的實體,可能是從另一個DataContext加載的實體。這不支持.' –

+0

查看我的編輯。你必須首先分離你的實體對象。 – Jan

3

我會認爲這是因爲您正在使用不同的上下文進行讀取和寫入。嘗試將您的DataClassesDataContext變量移至課程級別。

+0

這似乎是最好的解決方案[email protected],做到這一點,你已經足夠搖滾 –

+0

是的,如果這些方法屬於同一個班級,這很好。否則,Zruty的解決方案似乎更好。 –

+0

嗯....在這種情況下,你將不得不通過datacontext,但在一個類內,這是好得多 –

0

您不能使用++運算符並使用相同的上下文來更新對象。試試這個,

productToBeUpdated.setQuantity(productToBeUpdated.getQuantity()+1); 
+0

是的,這是因爲我在這裏寫了代碼。但其他任何財產不會更新。我在不同的項目中遇到過這種情況,這只是一個例子。 –

2

一種選擇是:使用通用的數據上下文,並將它傳遞給你的getXXX方法作爲參數:

public Object getProduct(DataClassesDataContext db, int categoryId, int productId) 
{ 
    switch (categoryId) 
    { 
     case CCategorii.CARTI_ID: 
      { 
       IEnumerable<Carti> product = (from c in db.Cartis 
               where c.Carti_id == productId 
               && c.Vizibil == true 
               select c); 

       if (product.Count() != 0) 
        return product.First(); 

       break; 
      } 
     //so on 
    } 
} 

然後:

public void updateProduct() 
{ 
    using (DataClassesDataContext db = new DataClassesDataContext(Settings.getDefaultConnectionStringName())) 
    { 
     Object productToBeUpdated = getProduct(db, 1,1); 
     //update some properties of the product 
     productToBeUpdated.setQuantity(productToBeUpdated.getQuantity()+1); // THX @AVD, didn't notice that. 
     db.submitChanges(); 
    } 
} 
+0

哦,沒有想到那個!很酷:) –

0

只要DataContext超出範圍,您的實體就會與其分離。這意味着它不再被您的上下文跟蹤,並且無法保存您對其所做的更改。

你可以分享的背景下這樣的實體不也會從你的上下文脫離,或者你可以將其安裝到第二上下文(DataContext.Attach

相關問題