Error "An attempt to track an entity or complex type failed because the entity or complex type"
I created an ASP.NET web application (to consume a WCF Data Service) and added a service reference to http://services.odata.org/Northwind/Northwind.svc. Create a web page (.aspx) and added a GridView and Button control.
Wrote the following code:
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")
开发者_C百科.Select(c => c);
DataServiceCollection<Customer> oCustomers = new DataServiceCollection<Customer>(q);
GridView1.DataSource = oCustomers;
GridView1.DataBind();
}
Once I execute the above code, I am experiencing the following error:
An attempt to track an entity or complex type failed because the entity or complex type 'NorthwindSvcRef.Customer' does not implement the INotifyPropertyChanged interface.
Can anyone help me on this?
Thanks in advance
Try using the overload DataServiceCollection<T> Constructor (IEnumerable<T>, TrackingMode)
and use the TrackingMode.None
enumerator.
e.g.
DataServiceCollection<MyTable> MyDataServiceCollection = new DataServiceCollection<MyTable>(qry, TrackingMode.None);
When you generate service reference using visual studio in the ASP.NET web app project, INotifyPropertyChange interface is not implemented in the generated proxy classes and that's why you are given this error. Simplest solution is to generate service refence in other project type (for example WPF or Windows Forms - INotifyPropertyChanged will be implemented automatically) and then copy "Reference.cs" file to your web app (you may need to change namespace inside this file). You must only remember not to generate service reference from web app project in the future because "Reference.cs" file will be overwritten and the error will return.
Specify the
config.useverboseerrors = true
in your service.cs
This will help you.
I had this problem, but it might be caused by the fact that my entities only had read rights, and therefore doesn't need to notify of any changes.
And the DataServiceCollection
provides notification of changes, thus the error occurring.
精彩评论