aaronmallen
aaronmallen

Reputation: 1438

Running a query on two DBcontext

I am trying to run a query against two context and I get this error everytime:

Unable to create a constant value of type 'CC_Metrics_Apps.RefQutReturns'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

I am attempting to query like so:

Public Function IDQ() As List(Of RefQutReturns)
  Dim val As List(Of RefQutReturns) = (From x In GlobalVar.db.RET.RefQutReturns
                                       Select x).ToList
  Return val
End Function

Public Function QueTypeDD() As IEnumerable(Of SelectListItem)
  Dim val As New List(Of SelectListItem)
  Dim valQ = From v In GlobalVar.db.PS.TaskQueueTypes
             Join x In IDQ() On v.QutID Equals x.QutID.ToString
             Select v
  val.Add(New SelectListItem With {.Text = "Select Item", .Value = 0})
  For Each v In valQ
    val.Add(New SelectListItem() With {.Text = v.QutDesc, .Value = v.QutID})
  Next
  Return val
End Function

Upvotes: 0

Views: 752

Answers (1)

Pawel
Pawel

Reputation: 31620

EF does not support queries spanning more than one context. In majority of cases it may not be possible to create and run a query like this in one database since it will have to access a different database (tracking entities would probably would also be problematic). If you have two contexts but work on one database you may want to merge the contexts. Otherwise you need to bring results for both contexts to the client and combine them using Linq To Objects query. Also note that in your case you are mixing Linq To Object query (because of .ToList() in the IDQ()) with Linq To Entities - this is also not supported since EF does not know how to bring the materialized non-primitive results to the database where the query will be executed.

Upvotes: 2

Related Questions