OData feed using EF 4.2 Code First and Fluent API to set Entity Key RRS feed

  • Question

  • I am trying to get a WCF Data Service up and running using a DBContext against an existing (semi) POCO model.  As my model is already built I am using the Fluent API to map my Table per Concrete Type (TPC) hierarchy (i.e. my Entities all share a set of core properties in an abstract base class - one of them being a Primary Key by the name Oid which is of type GUID).  In the DBContext's OnModelCreating() method I can configure the Entity Key field via the call

    modelBuilder.Entity<Widget>().HasKey(o => o.Oid); 

    My problem is when I go to open up the WCF DataService I get the message

    The exception message is 'On data context type 'EntitiesDbContext',
    there is a top IQueryable property 'Widgets' 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.

    I have tried a few things such as using the Ignore Properties calls to determine if some of the underlying properties aren't mapping to "known" Entities somehow

    modelBuilder.Entity<Widget>().Ignore(o => o.IsLoading);

    But from the looks of it and from what I've read it appears that the issue might be related to the Entity's Key not being determined.  The suggestion is to include the DataServiceKey attribute onto the POCO objects (which I would rather not do).  I guess I'm wondering since I've already specified the Key in the EF model via Code First using the HasKey() method, why do I also need to specify the DataServiceKey for the WCF Data Service.  This should already be know correct?  If anyone has ideas, I'm interested.


    Thursday, January 5, 2012 10:07 PM

All replies

  • Take a look at this: http://social.technet.microsoft.com/wiki/contents/articles/5234.aspx

    The DataService<T> must use an ObjectContext as the T and the blog shows how to override the CreateDataSource to get ObjectContext instance from your DbContext.

    Otherwise the DataService uses your DbContext as just a reflection provider based service and thus would require the DataServiceKey and would still not work well since the context is really EF.


    Vitek Karas [MSFT]
    Friday, January 6, 2012 7:19 AM
  • Thank you very much.  The link was very informative and I had not come across this before.  I definately now have a much better understanding of the working under the hood. 

    Unfortunately, I'm still not able to get my WCF DataService to pull up (albeit due to a new/different issue) possibly related to my other ORM which my existing objects were originally built upon.  I'm now suck with the error below which indicates issues with my Table per Concrete Type (TPC) functionality in the DbContext is not pulling through correctly even though I have my declarations as indicated in the code snippets to address the issue.  I haven't come across anything which indicates my PK property named Oid (of type GUID) can't be a primitive property either....

    The server encountered an error processing the request. The exception message is 'The property 'Oid' is not a declared property on type 'Widget'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.'. See server logs for more details. The exception stack trace is:

                modelBuilder.Entity<Widget>().Map(o =>
                modelBuilder.Entity<Widget>().HasKey(o => o.Oid);
                modelBuilder.Entity<Widget>().Property(o => o.Oid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    Friday, January 6, 2012 8:48 PM
  • Can you please include the stacktrace of the exception? What is the type of the exception? (need to know if this comes of WCF DS or EF).



    Vitek Karas [MSFT]
    Friday, January 6, 2012 9:44 PM