WCF Data Service 5.6 with Entity Framework 6.0 Complex Types


Question

I followed the post here to set up my WCF Data Service with Entity Framework 6.0: http://blogs.msdn.com/b/astoriateam/archive/2013/10/02/using-wcf-data-services-5-6-0-with-entity-framework-6.aspx

After converting the DataService to EntityFrameworkDataService, I'm not able to compile my project and this is because my call to CurrentDataSource does not translate all the methods on my Context. With regular DataService I was able to call CurrentDataSource.getEmployees() complex type and everything worked fine. However, with the new EntityFrameworkDataService getEmployees() is no longer available. What am I missing here?

Popular Answer

We tackled this by creating the DBContext, keeping it in a property on the service class and then injecting it into the service provider. Then any time we want to use it, we access the property.

protected override EntityFrameworkDataServiceProvider2<CustomDBContext> CreateDataSource()
{

    var dbContext = ContextHelper.GetContext();

    this.DBContext = dbContext;

    // Get the underlying ObjectContext for the DBContext. 
    var context = ((IObjectContextAdapter)this.DBContext).ObjectContext;
    context.ContextOptions.ProxyCreationEnabled = false;

    // Return the underlying context. 
    var args = new DataServiceProviderArgs(this, dbContext, {}, false);

    var provider = new EntityFrameworkDataServiceProvider2<CustomDBContext>(args);

    return provider;

}

Where CustomDBContext is the name of your context.

Then replace all your calls to CurrentDataSource with this.DBContext.





Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why