开发者

querying databases on same server with linq

In normal sql I could do joins on tables in different databases as long as they were on the same server (or linked servers). In linq I can't figure out how to do that. Is this possible? For example, if I have a database called db1 and another called db2. d开发者_运维百科b1 has a table called people and db2 has a table called address I could do something like...

select a.addressline1, p.firstname
from db1.dbo.people p
inner join db2.dbo.address a on p.peopleid = a.peopleid

Is this possible with linq? Thanks.


Multiple databases under a single context is not directly supported. Create views in the first database that point at tables in the second, and map entities to those views.

This article also shows this, and an alternative option by manually editing the datasource property:

http://damieng.com/blog/2010/01/11/linq-to-sql-tips-and-tricks-3


Personally when i need joins in LINQ-to-SQL i just make them in SQL. With LINQ they're rather difficult to write but it should be possible with the .JOIN selector in LINQ.

Another way to writing LINQ-queries is explained here


try using db1.ExecuteQuery(@"your query")

HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜