LINQPad,使用多个数据上下文

发布于 2024-08-06 19:17:22 字数 622 浏览 6 评论 0原文

我经常比较不同数据库中表中的数据。这些数据库具有相同的架构。在 TSQL 中,我可以使用 DB>user>table 结构(DB1.dbo.StoresDB2.dbo.OtherPlaces)来引用它们拉取数据进行比较。我非常喜欢 LINQPad 的想法,但我似乎无法轻松地从同一组语句中的两个不同数据上下文中提取数据。

我看到人们建议简单地更改连接字符串以将数据从其他源提取到当前模式中,但正如我所提到的,这是行不通的。我是否只是跳过了常见问题解答中的一页?这似乎是一个相当常规的程序,对我来说无法实现。

在“简单”的世界中,我希望能够简单地引用 LINQPad 创建的类型化数据上下文。然后我可以简单地:

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

然后从那里开始工作。

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. 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.

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. Then I could simply:

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

And work from there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

半寸时光 2024-08-13 19:17:22

更新:现在可以在 LINQPad 中执行跨数据库 SQL Server 查询(从 LINQPad v4.31 开始,具有 LINQPad Premium 许可证)。要使用此功能,请按住 Control 键,同时将数据库从架构资源管理器拖动到查询窗口。

还可以查询链接服务器(您通过调用sp_add_linkedserver链接的服务器)。为此:

  1. 添加新的 LINQ to SQL 连接。
  2. 选择指定新的或现有的数据库并选择您要查询的主数据库。
  3. 单击包括其他数据库复选框,然后从列表中选择链接服务器。

Update: it's now possible to do cross-database SQL Server queries in LINQPad (from LINQPad v4.31, with a LINQPad Premium license). To use this feature, hold down the Control key while dragging databases from the Schema Explorer to the query window.

It's also possible to query linked servers (that you've linked by calling sp_add_linkedserver). To do this:

  1. Add a new LINQ to SQL connection.
  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.
赴月观长安 2024-08-13 19:17:22

请记住,您始终可以自己创建另一个上下文。

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());
}

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());
}
画中仙 2024-08-13 19:17:22

您可以实例化任意多个上下文来分散 SQL 实例并执行伪跨数据库联接、复制数据等。请注意,跨上下文的联接是在本地执行的,因此您必须调用 ToList()、ToArray() 等来执行查询在加入之前单独使用各自的数据源。换句话说,如果您“内部”连接 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();
        
remoteContext.Database.Connection.Close();
remoteContext = null;

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();
        
remoteContext.Database.Connection.Close();
remoteContext = null;
网名女生简单气质 2024-08-13 19:17:22

我认为你无法做到这一点。请参阅此 LinqPad 请求。< /a>

但是,您可以在单独的 dll 中构建多个 dbml 文件并在 LinqPad 中引用它们。

I do not think you are able to do this. See this LinqPad request.

However, you could build multiple dbml files in a separate dll and reference them in LinqPad.

蓝颜夕 2024-08-13 19:17:22

拖放方法:按住 Ctrl 键拖动其他数据库
从架构资源管理器到查询编辑器。

使用案例:

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

Drag-and-drop approach: hold down the Ctrl key while dragging additional databases
from the Schema Explorer to the query editor.

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();
调妓 2024-08-13 19:17:22

据我所知,多个数据库仅在 LinqPad 的“付费”版本中可用(我编写的内容适用于 LinqPad 6 Premium)。

有关更多详细信息,请参阅StackOverflow 中的此答案“多数据库支持”部分)。

Multiple databases are as far as I know only available in the "paid" version of LinqPad (what I wrote applies to LinqPad 6 Premium).

For more details, see this answer in StackOverflow (section "Multiple database support").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文