简体   繁体   English

LINQPad,使用多个数据上下文

[英]LINQPad, using multiple datacontexts

I am often comparing data in tables in different databases.我经常比较不同数据库中表中的数据。 These databases do not have the same schema.这些数据库没有相同的架构。 In TSQL, I can reference them with the DB>user>table structure ( DB1.dbo.Stores , DB2.dbo.OtherPlaces ) to pull the data for comparison.在 TSQL 中,我可以使用DB>user>table结构( DB1.dbo.StoresDB2.dbo.OtherPlaces )引用它们来提取数据进行比较。 I like the idea of LINQPad quite a bit, but I just can't seem to easily pull data from two different data contexts within the same set of statements.我非常喜欢 LINQPad 的想法,但我似乎无法轻松地从同一组语句中的两个不同数据上下文中提取数据。

I've seen people suggest simply changing the connection string to pull the data from the other source into the current schema but, as I mentioned, this will not do.我见过有人建议简单地更改连接字符串以将数据从其他源提取到当前模式中,但正如我所提到的,这是行不通的。 Did I just skip a page in the FAQ?我是否只是跳过了常见问题解答中的一个页面? This seems a fairly routine procedure to be unavailable to me.这对我来说似乎是一个相当常规的程序。

In the "easy" world, I'd love to be able to simply reference the typed datacontext that LINQPad creates.在“简单”的世界中,我希望能够简单地引用 LINQPad 创建的类型化数据上下文。 Then I could simply:然后我可以简单地:

DB1DataContext db1 = new DB1DataContext();
DB2DataContext db2 = new DB2DataContext();

And work from there.从那里开始工作。

Update : it's now possible to do cross-database SQL Server queries in LINQPad (from LINQPad v4.31, with a LINQPad Premium license). 更新 :现在可以在LINQPad中进行跨数据库SQL Server查询(来自LINQPad v4.31,具有LINQPad Premium许可证)。 To use this feature, hold down the Control key while dragging databases from the Schema Explorer to the query window. 要使用此功能,请在将数据库从“架构资源管理器”拖动到查询窗口时按住Control键。

It's also possible to query linked servers (that you've linked by calling sp_add_linkedserver ). 也可以查询链接的服务器 (通过调用sp_add_linkedserver 链接的服务器 )。 To do this: 去做这个:

  1. Add a new LINQ to SQL connection. 添加新的LINQ to SQL连接。
  2. Choose Specify New or Existing Database and choose the primary database you want to query. 选择“ 指定新数据库”或“现有数据库”,然后选择要查询的主数据库。
  3. Click the Include Additional Databases checkbox and pick the linked server(s) from the list. 单击“ 包括其他数据库”复选框,然后从列表中选择链接的服务器。

Keep in mind that you can always create another context on your own. 请记住,您始终可以自己创建另一个上下文。

public FooEntities GetFooContext()
{
   var entityBuilder = new EntityConnectionStringBuilder        
               {        
                    Provider = "Devart.Data.Oracle",        
                    ProviderConnectionString = "User Id=foo;Password=foo;Data Source=Foo.World;Connect Mode=Default;Direct=false",
                    Metadata = @"D:\FooModel.csdl|D:\FooModel.ssdl|D:\FooModel.msl"     
                };

    return new FooEntities(entityBuilder.ToString());
}

I do not think you are able to do this. 我不认为你能做到这一点。 See this LinqPad request. 请参阅此LinqPad请求。

However, you could build multiple dbml files in a separate dll and reference them in LinqPad. 但是,您可以在单独的dll中构建多个dbml文件,并在LinqPad中引用它们。

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;

Multiple databases are as far as I know only available in the "paid" version of LinqPad (what I wrote applies to LinqPad 6 Premium).据我所知,多个数据库仅在 LinqPad 的“付费”版本中可用(我写的内容适用于 LinqPad 6 Premium)。

For more details, see this answer in StackOverflow (section "Multiple database support" ).有关更多详细信息,请参阅StackOverflow 中的此答案“多数据库支持”部分)。

Drag-and-drop approach: hold down the Ctrl key while dragging additional databases from the Schema Explorer to the query editor. 拖放方法:按住Ctrl键,同时将其他数据库从Schema Explorer拖到查询编辑器。

Use case: 使用案例:

//Access Northwind

var ID = new Guid("107cc232-0319-4cbe-b137-184c82ac6e12");

LotsOfData.Where(d => d.Id == ID).Dump();

//Access Northwind_v2

this.NORTHWIND_V2.LotsOfData.Where(d => d.Id == ID).Dump();

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

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