Reputation: 3661
I have bought the premium version of LINQPad. I thought it would also be possible to perform cross data base queries with DevForce models.
There are two ways to do this. The simplest is the drag-and-drop approach: hold down the Ctrl key while dragging additional databases from the Schema Explorer to the query editor. To access those additional databases in your queries, use database.table notation, e.g., Northwind.Regions.Take(100). The databases that you query must reside on the same server.
The second approach is to list the extra database(s) that you want to query in the connection properties dialog. This dialog also lets you choose databases from linked servers. Here's how to proceed:
- Add a new LINQ to SQL connection.
- Choose Specify New or Existing Database and choose the primary database that you want to query.
- Click the Include Additional Databases checkbox and pick the extra database(s) you want to include. You can also choose databases from linked servers in this dialog.
But obviously there isn't any way, is it? Anyone a solution for this?
Upvotes: 0
Views: 2393
Reputation:
You can instantiate as many contexts as you like to disparate SQL instances and execute pseudo cross database joins, copy data, etc. Note, joins across contexts are performed locally so you must call ToList(), ToArray(), etc to execute the queries using their respective data sources individually before joining. In other words if you "inner" join 10 rows from DB1.TABLE1 with 20 rows from DB2.TABLE2, both sets (all 30 rows) must be pulled into memory on your local machine before Linq performs the join and returns the related/intersecting set (20 rows max per example).
//EF6 context not selected in Linqpad Connection dropdown
var remoteContext = new YourContext();
remoteContext.Database.Connection.ConnectionString = "Server=[SERVER];Database="
+ "[DATABASE];Trusted_Connection=false;User ID=[SQLAUTHUSERID];Password="
+ "[SQLAUTHPASSWORD];Encrypt=True;";
remoteContext.Database.Connection.Open();
var DB1 = new Repository(remoteContext);
//EF6 connection to remote database
var remote = DB1.GetAll<Table1>()
.Where(x=>x.Id==123)
//note...depending on the default Linqpad connection you may get
//"EntityWrapperWithoutRelationships" results for
//results that include a complex type. you can use a Select() projection
//to specify only simple type columns
.Select(x=>new { x.Col1, x.Col1, etc... })
.Take(1)
.ToList().Dump(); // you must execute query by calling ToList(), ToArray(),
// etc before joining
//Linq-to-SQL default connection selected in Linqpad Connection dropdown
Table2.Where(x=>x.Id = 123)
.ToList() // you must execute query by calling ToList(), ToArray(),
// etc before joining
.Join(remote, a=> a.d, b=> (short?)b.Id, (a,b)=>new{b.Col1, b.Col2, a.Col1})
.Dump();
localContext.Database.Connection.Close();
localContext = null;
Upvotes: 1
Reputation: 30964
Cross-database querying works only with standard SQL Server connections, with databases on the same server or on linked servers. The main rationale is to ensure server-side joining (otherwise you'd end up pulling entire tables back to the client whenever you joined).
I have considered adding a feature to LINQPad to allow arbitrary cross-database queries, because sometimes it would be useful even with client-side joining. However, getting this to work with custom data contexts (such as DevForce or Entity Framework) turned out to be really tricky, and so the feature ended up in the "too-hard basket". A major problem was dealing with namespace/assembly/app.config conflicts.
Bear in mind that there's nothing to stop you from pressing F4 and adding a reference to an assembly containing an additional datacontext. Of course, you'd have to manually instantiate the second data context, but that shouldn't be a huge deal. You'll still get autocompletion, and you'll still be able to see its schema in the tree view if you create a separate connection for it. And functionally, that's what you'd end up with anyway, if LINQPad supported multi-connection queries.
What's special about LINQPad's cross-database querying support for SQL Server is that it does something you couldn't otherwise do simply by adding a reference to another assembly, which is to allow efficient cross-database querying by leveraging server-side joins.
Upvotes: 2