by JasonRShaver
29. August 2011 15:53
I came across this error a few times while working on an Code First/POCO style Entity Framework 4 (EF4) project. The goal was to create a simple EF4/Code First/SQL CE/oData/WP7.1 application, but I got caught up on the setup of the WCF Data Services part. Here is the error:
The server encountered an error processing the request. The exception message is 'On data context type 'ProductContext', there is a top IQueryable property 'Products' whose element type is not an entity type. Make sure that the IQueryable property is of entity type or specify the IgnoreProperties attribute on the data context type to ignore this property.'. See server logs for more details. The exception stack trace is:
at System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 childTypes, IDictionary`2 entitySets) at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata() at System.Data.Services.DataService`1.CreateProvider() at System.Data.Services.DataService`1.HandleRequest() at
[…] You get the point
So, I started double checking everything, and making sure WCF Data Services supported POCO objects (it does), and I ended up with something simple. All entities must have a primary key, while my code looked like such:
namespace ProductCatalog.Website.Entities
{
public class Product
{
public String Name { get; set; }
public String PhotoUrl { get; set; }
}
public class ProductContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
}
So, I added the DataServiceKey attribute to the name like so:
namespace ProductCatalog.Website.Entities
{
[DataServiceKey(“Name”)]
public class Product
{
[Key]
public String Name { get; set; }
public String PhotoUrl { get; set; }
}
public class ProductContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
}
And I was good to go.
1b6b4fbb-7172-4f0d-9c09-4aab56579d64|2|5.0
Tags: