2017-08-07 73 views
0

文脈傳承一個實體類型多個數據庫上下文?

// MobileContext has VisitPlan, Customer, etc. that all the following contexts need. 
public MobileContext : DbContext { } 
public AuthenticationEntities : MobileContext { } 
public OrderEntities : MobileContext { } 
public ThriftEntities : MobileContext { } 

的上下文是代碼,第一,我不使用它們來創建數據庫。

說明

我創造出具有存儲庫VisitPlanUserBranch,等所有的庫中的UserPermissionService一個實例是在AuthenticationEntitiesCustomerVisitPlan是的MobileContextAuthenticationEntites的一部分,其他情況從...繼承。

問題

當我嘗試執行聯接查詢UserBranchVisitPlan它告訴我,我不能在兩個上下文之間的查詢,但如果我看在調試器在它們兩者的庫的DbContext鍵入AuthenticationEntities

有沒有辦法做到這一點?


查詢

// 
// USER-BRANCHES: Check the user branches table for permissions. 
// 
var branches = Branches 
    .GetAll() 
    .Where(x => x.AzureUser.Username == username && x.StartDate <= effective && x.EndDate >= effective); 

// 
// USER-ROUTES: Check the user routes table for permissions. 
// 
var routes = Routes 
    .GetAll() 
    .Where(x => x.AzureUser.Username == username && x.StartDate <= effective && x.EndDate >= effective); 

// 
// USER-DRIVERS: Check the user driver number table for permissions. 
// 
var drivers = DriverNumbers 
    .GetAll() 
    .Where(x => x.AzureUser.Username == username && x.StartDate <= effective && x.EndDate >= effective); 

// 
// VISIT PLANS: Retrieve a list of visit plans currently active. 
// 
var vpQuery = VisitPlans 
    .GetAll() 
    .Where(
     x => x.FromDate <= effective && x.ToDate >= effective 
      && (
       branches.Any(b => b.Branch == x.Branch) 
       || routes.Any(r => r.Route == x.Route) 
       || drivers.Any(d => d.DriverNumber == x.DriverNumber) 
     ) 
    ); 

// 
// QUERY: Retrieve all the customers which have effective stop plans and are included in one of the above queries. 
// 
var customerQuery = vpQuery 
    .Join(
     inner: Customers.GetAll(), 
     outerKeySelector: x => x.SAPCustomerID, 
     innerKeySelector: x => x.SAPCustomerID, 
     resultSelector: (vp, c) => c 
    ); 

其中:

  • VisitPlansRepository<VisitPlan>類型其使用AuthenticationEntities作爲其DbContext
  • Customers的是類型的10,它使用AuthenticationEntities作爲其DbContext
  • BranchesRepository<UserBranch>型其使用AuthenticationEntities作爲其DbContext
  • Routes的是Repository<UserRoute>類型其使用AuthenticationEntities作爲其DbContext
  • DriverNumbersRepository<UserDriverNumber>類型其使用AuthenticationEntities的作爲它的DbContext
+1

請分享您的查詢碼。您應該有一個負責查詢的DbContext實例,以及從該查詢返回的所有實體。你不能連接不同的'DbContext'實例(*我也不知道你爲什麼想要*)。 – Igor

+0

如果我添加查詢代碼,它將需要一堆其他代碼,頁面將長達一英里。查詢不是問題,問題是我在所有上下文中都有一些實體(即VisitPlan等),當我在一個特定上下文(例如UserBranch,UserRoute等)的實體和實體之間加入時,它抱怨兩個上下文,即使這些存儲庫正在使用「AuthenticationEntities」的實例。所以對我來說,似乎有一個與'VisitPlan'和其他實體在多個上下文的問題,這就是我所問。 – Shelby115

+0

@ Shelby115這聽起來像你需要重新考慮你的設計。除非有特殊情況,例如擁有多個數據庫,否則您應該只有一個上下文。如果您正在使用存儲庫模式(實體框架不需要這種模式),則應該在需要時注入上下文。 – JNYRanger

回答

1

它並不重要在他們是相同的類型。您必須使用您的DbContext的單個實例。在使用實體框架時,必須在單個上下文實例上執行單個查詢(本例中爲Join)。

理想情況下,你應該只使用一個DbContext實例是負責使所有查詢的數據庫。如果您使用多個數據庫,那麼您將有多個實例(對於單個請求)的唯一原因是。

然而,讓我們假設你需要有多個上下文對象。你需要做的是查詢每個上下文並將結果存入內存。這可以通過在每個查詢結束時調用.ToList()來完成。

一旦數據是內存,您可以將它們連接在一起。下面是一個例子:

var vpQuery = authContext.VisitPlan.Where(x => x == something).ToList(); 
var ubQuery = permissionContext.UserBranch.Where(u => u == somethingElse).ToList(); 
var joined = vpQuery.Join(vpQuery, vp => vp.UserKey, ub => ub.UserKey, (vp, ub) => new { Property1 = ub.Something, Property2 = vp.SomethingElse); 

但是,根據您發佈的內容,您絕對不需要多個上下文實例。很可能您的存儲庫代碼(可能不需要)持有或創建與其他存儲庫的上下文對象不同的上下文。如果您想使用延遲加載,並且實際上僅在需要時才生成執行查詢,則它們應該都共享單個上下文實例。

相關問題