简体   繁体   English

LINQ to SQL两个上下文

[英]LINQ to SQL two contexts

I'm trying to get data from two different database to retrieve data. 我正在尝试从两个不同的数据库中获取数据以检索数据。 , However; ,但是; I get this error. 我得到这个错误。

**The query contains references to items defined on a different data context**

    db = context1
    db2 = context2

    var query = from m in db.Merch
                join d in db.Dept on m.merchDept equals d.deptID.ToString()
                join p in db2.PROD_SKU on m.pID equals p.pID
                join f in db.FOB on d.fobCode equals f.fobID
                join w in db.Item on m.merchID equals w.merchID
                join i in db.Doc on m.merchID equals i.MerchID
                            where p.UPC_NBR.ToString() == upc

How would I go about using these two contexts to retrieve this record using linq. 我将如何使用这两个上下文使用linq检索此记录。 Any help would be appreciated. 任何帮助,将不胜感激。

Linq2Sql does not support queries on multiple database context. Linq2Sql不支持对多个数据库上下文的查询。 However, your example looks pretty simple - find p.pID by upc and then use the value as an argument in second query. 但是,您的示例看起来非常简单-通过upc查找p.pID,然后在第二个查询中将该值用作参数。 It may look like this: 它可能看起来像这样:

db = context1
db2 = context2

var upcId = (from p in db2.PROD_SKU 
            where p.UPC_NBR.ToString() == upc
            select p.pID).Single();

var query = from m in db.Merch
            join d in db.Dept on m.merchDept equals d.deptID.ToString()         
            join f in db.FOB on d.fobCode equals f.fobID
            join w in db.Item on m.merchID equals w.merchID
            join i in db.Doc on m.merchID equals i.MerchID
                        where m.pID == upcId;

The short answer is that you can't... not easily. 简短的答案是,您不能……不容易。 In order to do a cross-database join, you have to use one Data Context that references both databases. 为了进行跨数据库联接,您必须使用一个引用两个数据库的数据上下文。

  <Table Name="db.dbo.Merch" Member="Merch">
    ...
  </Table>
  <Table Name="db2.dbo.PROD_SKU" Member="PROD_SKU">
    ...
  </Table>

In your DBML, manually change the names of your tables to look something like the above. 在您的DBML中,手动更改表名,使其类似于上面的内容。 You also have to make sure that you've set up permissions to allow querying both databases. 您还必须确保已设置权限以允许查询两个数据库。

For a more complicated scenario, you can also execute two LINQ queries and join the results. 对于更复杂的情况,您还可以执行两个LINQ查询并合并结果。 Here I acquired the history of status updates to sales quotes and joined them with the table in another database that contains the updating users' names, so I could display them in an ItemsControl: 在这里,我获取了销售报价状态更新的历史记录,并将它们与另一个数据库中包含更新用户名的数据库中的表结合在一起,因此可以在ItemsControl中显示它们:

var firstResults = (from qsl in firstContext.QuoteStatusLogs
                    join qs in firstContext.QuoteStatus
                    on qsl.QuoteStatusID equals (qs.QuoteStatusID)
                    where qsl.QuoteID == quoteID
                    select new
                    {
                        HistoryEvent = qsl.UpdateTime,
                        QuoteStatus = qs.Description,
                        UserGUID = qsl.EmployeeGUID,
                        Comment = qsl.Comment
                    }).ToList();

var finalResults = (from r1 in firstResults
                    join e in secondContext.Employees
                    on r1.UserGUID equals (e.UserID)
                    orderby r1.HistoryEvent descending
                    select new QuoteStatusHistoryResult
                    {
                        HistoryEvent = r1.HistoryEvent,
                        QuoteStatus = r1.QuoteStatus,
                        User = e.FirstName + " " + e.LastName,
                        Comment = r1.Comment
                    }).ToList();

statusHistory = new ObservableCollection<QuoteStatusHistoryResult>(finalResults);

That first "ToList" call is important, as without it I got an exception saying I was trying to query from two contexts. 第一个“ ToList”调用很重要,因为如果没有它,我会遇到一个异常,说我试图从两个上下文中查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM