带有Entity Framework 6.0复杂类型的WCF数据服务5.6

时间:2013-11-02 01:18:08

标签: wcf-data-services entity-framework-6

我按照此处的帖子使用Entity Framework 6.0设置我的WCF数据服务:http://blogs.msdn.com/b/astoriateam/archive/2013/10/02/using-wcf-data-services-5-6-0-with-entity-framework-6.aspx

将DataService转换为EntityFrameworkDataService后,我无法编译我的项目,这是因为我对CurrentDataSource的调用没有翻译我的Context上的所有方法。使用常规DataService,我能够调用CurrentDataSource.getEmployees()复杂类型,一切正常。但是,使用新的EntityFrameworkDataService,getEmployees()不再可用。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我们通过创建DBContext来解决这个问题,将其保存在服务类的属性中,然后将其注入服务提供者。然后,只要我们想要使用它,我们就会访问该属性。

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;

}

其中CustomDBContext是您的上下文的名称。

然后用this.DBContext替换你对CurrentDataSource的所有调用。

相关问题