2011-08-29 67 views
1

我創建了ASP.NET Web應用程序(以使用WCF數據服務)並添加了對http://services.odata.org/Northwind/Northwind.svc的服務引用。創建一個網頁(.aspx)並添加一個GridView和Button控件。錯誤「試圖跟蹤實體或複雜類型失敗,因爲實體或複雜類型」

寫了下面的代碼:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    var o = new NorthwindSvcRef.NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc")); 

    //The following works fine 
    //------------------------ 
    //var q = o.Customers.Where(c => c.City == "London").ToList(); 
    //GridView1.DataSource = q; 
    //GridView1.DataBind(); 


    //Following does not work 
    //----------------------- 
    var q = o.Customers 
     .Where(c => c.City == "London") 
     .Select(c => c); 

    DataServiceCollection<Customer> oCustomers = new DataServiceCollection<Customer>(q); 
    GridView1.DataSource = oCustomers; 
    GridView1.DataBind(); 
} 

一旦我執行上面的代碼中,我遇到了以下錯誤:

An attempt to track an entity or complex type failed because the entity or complex type 'NorthwindSvcRef.Customer' does not implement the INotifyPropertyChanged interface.

誰能幫助我在這?

在此先感謝

+0

您使用的是什麼版本的WCF數據服務?簡而言之,DataServiceCollection 要求您提供它的實體實現數據綁定接口= INotifyPropertyChanged。添加服務引用應該爲您負責,但只有某些版本才能正確執行此操作。 –

+0

我正在使用Visual Studio 2010 Ultimate(與目標框架4.0) – user203687

回答

0

當您在ASP.NET Web應用程序項目中使用Visual Studio生成服務引用時,INotifyPropertyChange接口未在生成的代理類中實現,這就是爲何出現此錯誤的原因。最簡單的解決方案是在其他項目類型(例如WPF或Windows窗體 - 將自動實現INotifyPropertyChanged)中生成服務請求,然後將「Reference.cs」文件複製到您的Web應用程序(您可能需要更改此文件中的命名空間)。您必須記住將來不要從Web應用程序項目生成服務引用,因爲「Reference.cs」文件將被覆蓋並且錯誤將會返回。

0

service.cs

這將幫助你指定

config.useverboseerrors = true

+0

沒有特殊消息,我可以看到 – user203687

0

我有這個問題,但可能是由於我的實體只具有讀取權限,因此不需要通知任何更改。

DataServiceCollection提供更改通知,從而發生錯誤。

相關問題