2017-08-07 72 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); 

但是,根据您发布的内容,您绝对不需要多个上下文实例。很可能您的存储库代码(可能不需要)持有或创建与其他存储库的上下文对象不同的上下文。如果您想使用延迟加载,并且实际上仅在需要时才生成执行查询,则它们应该都共享单个上下文实例。

相关问题