user203687
user203687

Reputation: 7237

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")
       .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

Upvotes: 1

Views: 1376

Answers (4)

F Snyman
F Snyman

Reputation: 509

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);

Upvotes: 3

Marcusdev
Marcusdev

Reputation: 1

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.

Upvotes: 0

nexus
nexus

Reputation: 16

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.

Upvotes: 0

Chintan Udeshi
Chintan Udeshi

Reputation: 1

Specify the

config.useverboseerrors = true in your service.cs

This will help you.

Upvotes: 0

Related Questions