简体   繁体   English

LinqPad,使用多个数据上下文-DevForce

[英]LinqPad, using multiple datacontexts - DevForce

I have bought the premium version of LINQPad. 我已经购买了LINQPad的高级版。 I thought it would also be possible to perform cross data base queries with DevForce models. 我认为使用DevForce模型执行跨数据库查询也是可能的。

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. 最简单的方法是拖放:在将其他数据库从“模式资源管理器”拖到查询编辑器时,按住Ctrl键。 To access those additional databases in your queries, use database.table notation, eg, Northwind.Regions.Take(100). 要在查询中访问那些其他数据库,请使用database.table表示法,例如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: 操作方法如下:

  1. Add a new LINQ to SQL connection. 将新的LINQ添加到SQL连接。
  2. Choose Specify New or Existing Database and choose the primary database that you want to query. 选择“指定新数据库或现有数据库”,然后选择要查询的主数据库。
  3. 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. 您也可以在此对话框中从链接服务器中选择数据库。

Source 资源

But obviously there isn't any way, is it? 但是显然没有办法,对吗? Anyone a solution for this? 有人对此有解决方案吗?

Cross-database querying works only with standard SQL Server connections, with databases on the same server or on linked servers. 跨数据库查询仅适用于标准SQL Server连接,同一服务器或链接服务器上的数据库。 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. 我考虑过向LINQPad添加一个功能以允许任意跨数据库查询,因为有时即使在客户端连接中它也很有用。 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". 但是,将其与自定义数据上下文(例如DevForce或Entity Framework)一起使用确实很棘手,因此该功能最终陷入了“过于艰难的境地”。 A major problem was dealing with namespace/assembly/app.config conflicts. 一个主要问题是处理名称空间/assembly/app.config冲突。

Bear in mind that there's nothing to stop you from pressing F4 and adding a reference to an assembly containing an additional datacontext. 请记住,没有什么可以阻止您按F4键并向包含附加数据上下文的程序集添加引用。 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. 从功能上讲,如果LINQPad支持多连接查询,那最终将是您的最终选择。

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. LINQPad对SQL Server的跨数据库查询支持的特殊之处在于,它通过简单地添加对另一个程序集的引用就可以完成您无法做的事情,即通过利用服务器端联接来实现高效的跨数据库查询。

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. 您可以实例化任意多个SQL实例,并执行伪跨数据库联接,复制数据等。请注意,跨上下文的联接是在本地执行的,因此必须调用ToList(),ToArray()等来执行查询。在加入之前,分别使用它们各自的数据源。 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). 换句话说,如果您“内部”联接DB1.TABLE1的10行和DB2.TABLE2的20行,则必须在Linq执行联接并返回相关/相交之前,将这两个集合(全部30行)都拉入本地计算机上的内存中。设置(每个示例最多20行)。

//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;

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

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