簡體   English   中英

一個實體類型有多個數據庫上下文?

[英]One entity type multiple database contexts?

上下文繼承

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

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

描述

我創建了一個UserPermissionService實例,該實例具有用於VisitPlanUserBranch等的存儲庫。所有存儲庫都位於AuthenticationEntities其中CustomerVisitPlanMobileContext的一部分, AuthenticationEntites和所有其他上下文都繼承VisitPlan

問題

當我嘗試執行將UserBranchVisitPlan連接UserBranch的查詢時,它告訴我無法在兩個上下文之間進行查詢,但是如果在調試器中DbContext存儲庫的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
    );

哪里:

  • VisitPlans的類型為Repository<VisitPlan> ,它使用AuthenticationEntities作為其DbContext
  • Customers的類型為Repository<Customer> ,它使用AuthenticationEntities作為其DbContext
  • Branches的類型為Repository<UserBranch> ,它使用AuthenticationEntities作為其DbContext
  • Routes的類型為Repository<UserRoute> ,它使用AuthenticationEntities作為其DbContext
  • DriverNumbers的類型為Repository<UserDriverNumber> ,它使用AuthenticationEntities作為其DbContext

它們是相同類型沒關系。 您必須使用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);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM