2013-11-02 111 views
0

我也跟着張貼在這裏建立我的WCF數據服務與實體框架6.0:http://blogs.msdn.com/b/astoriateam/archive/2013/10/02/using-wcf-data-services-5-6-0-with-entity-framework-6.aspxWCF數據服務5.6與實體框架6.0複雜類型

DataService在轉換爲EntityFrameworkDataService後,我不能編譯我項目,這是因爲我對CurrentDataSource的調用不會轉換我的上下文中的所有方法。使用常規DataService,我可以調用CurrentDataSource.getEmployees()複雜類型,並且一切正常。但是,新的EntityFrameworkDataService getEmployees()不再可用。我在這裏錯過了什麼?

+0

看起來我不是唯一有這個問題的人... https://social.msdn.microsoft.com/Forums/en-US/59aba0c8-25bc-4c67-b1c8-f2086fe94dfa/56ef6actions-broken? forum = adodotnetdataservices –

+0

[相關問題報告](https://data.uservoice.com/forums/72027-wcf-data-services-feature-suggestions/suggestions/4734171-retrieve-dbcontext-in-entityframeworkdataservice)(以前是答案,但由於它僅包含鏈接而被刪除)。 – tne

回答

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的所有調用。