2012-04-22 25 views
1

編輯:得到它排序,該表有外鍵沒有被解決。 Stackoverflow不會讓我發佈一個答案這個接近問這個問題ObjectContext實例已被處置,不能再用於需要連接的操作。 WCF與天藍色錯誤

我正在做一個Windows Phone應用程序,它從Windows Azure上的數據庫獲取信息,我使用WCF連接來做所以。

但是當我調試和本地調用我得到以下錯誤的服務:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

我使用的代碼,我改編自http://mobile.dzone.com/news/how-use-wcf-services-access-0(該LoginUser部分),從該網站的代碼工作正常,但,就像我說的那樣,我的原因是這個錯誤。

這裏是我的代碼:

public Product GetProduct(String Barcode) 
    { 
     string query = @"SELECT value Product FROM AzureDBEntities.Products AS Product WHERE Product.barcode = @Barcode"; 
     ObjectParameter parameter = new ObjectParameter("Barcode", Barcode); 

     using (var context = new AzureDBEntities()) 
     { 
      ObjectQuery<Product> results = context.CreateQuery<Product>(query, parameter); 

      foreach (Product result in results) 
      { 
       if (result != null) 
       { 
        return result; 
       } 
      } 
     } 
     return null; 
    } 

任何想法我做了什麼錯?非常感謝。

+0

什麼是堆棧跟蹤? – SLaks 2012-04-22 16:07:53

+0

WCFServiceWebRole1.dll!WCFServiceWebRole1.Product.Producer1.get()第1028行+ 0x34字節 這是你需要的嗎? – IntSec 2012-04-22 16:21:31

回答

1

在您的回答你的狀態是,埃羅由以下引起評論:WCFServiceWebRole1.dll WCFServiceWebRole1.Product.Producer1.get()

這表明一些試圖訪問生產者產品實體上的屬性(可能是由WCF serializaiton引起的)。你可以嘗試關閉你的上下文中的延遲加載和代理創建嗎?

using (var context = new MyEntities()) 
{ 
    context.ContextOptions.LazyLoadingEnabled = false; 
    context.ContextOptions.ProxyCreationEnabled = false; 
} 
2

通過它的detach方法將實體傳遞給ObjectContext的一個實例。這會破壞您的導航集合,但您不必關閉延遲加載。

public Product GetProduct(String Barcode) 
{ 
    string query = @"SELECT value Product FROM AzureDBEntities.Products AS Product WHERE Product.barcode = @Barcode"; 
    ObjectParameter parameter = new ObjectParameter("Barcode", Barcode); 

    using (var context = new AzureDBEntities()) 
    { 
     ObjectQuery<Product> results = context.CreateQuery<Product>(query, parameter); 

     foreach (Product result in results) 
     { 
      if (result != null) 
      { 
       context.Detach(result); 
       return result; 
      } 
     } 
    } 
    return null; 
} 
相關問題