2011-08-29 38 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提供更改通知,从而发生错误。

相关问题