简体   繁体   English

如何使用Linq连接不同数据库中的表?

[英]How do you use Linq to connect tables in different databases?

I'm a bit of a Linq newbie, and I couldn't find any documentation to help me with what seems to be a pretty trivial problem - so your help will be much appreciated! 我有点像Linq新手,而且我找不到任何文档可以帮助我解决看似非常微不足道的问题 - 所以我们非常感谢您的帮助!

I have a table Table1 in database DB1 , which has a "pseudo" foreign key Table2ID to table Table2 in database DB2 , on the same server. 我在数据库DB1有一个表Table1 ,它在同一服务器上的数据库DB2Table2中有一个“伪”外键Table2ID "Pseudo", because obviously I can't have an actual FK spanning two databases. “Pseudo”,因为很明显我不能拥有跨越两个数据库的实际 FK。

Now I'm playing around with the O/R designer, and I love the way all the relationships are generated when I bring database objects into the designer... very cool! 现在我正在和O / R设计师一起玩,我喜欢将数据库对象带入设计师时生成所有关系的方式......非常酷! And I want my Table1 object to have a relationship to Table2 , just like it has relationships with all the "real" foreign key-related objects in DB1 . 我希望我的Table1对象与Table2有关系,就像它与DB1所有“真正的”外键相关对象有关系一样。 But I can't bring Table2 into my db diagram, because it's in the wrong DB. 但我无法将Table2带入我的数据库图表中,因为它位于错误的数据库中。

To synthesize this, I tried creating a view Table2 in DB1 , which is simply select * from DB2..Table2 . 为了综合这个,我尝试在DB1创建一个视图Table2 ,它只是select * from DB2..Table2 Aha, now I can drop a Table2 object into my diagram. 啊哈,现在我可以将Table2对象放入我的图表中。 I can even make a parent/child relationship between Table1 and Table2 . 我甚至可以在Table1Table2之间建立父/子关系。 But when I look at the generated code, Table1 still has no relationship to Table2 , which I find most perplexing. 但是当我查看生成的代码时, Table1仍然与Table2没有任何关系,我觉得最令人困惑。

Am I missing a step somewhere? 我在某处错过了一步吗? Is there a better/recommended way of doing this? 有没有更好的/推荐的方式这样做?

Thanks! 谢谢!


Later... 后来...

Along the lines of what one person suggested, I tried filling in the partial class of Table1 with all the methods required to access Table2 , by copying all the structures for a related object within the same DB. 根据一个人的建议,我尝试通过复制同一个DB中相关对象的所有结构来填充Table1的部分类,其中包含访问Table2所需的所有方法。

This actually worked for reads, but as soon as I tried to update or insert a record, I got an exception: 这实际上适用于读取,但是当我尝试更新或插入记录时,我得到了一个例外:

An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.

So it looks like the designers of Linq have actually thought about this scenario, and decided that you are not allowed to connect objects in different databases. 所以看起来Linq的设计师实际上已经考虑过这种情况,并决定不允许连接不同数据库中的对象。 That's really a shame... :( 真是个耻辱...... :(


... and even later... ......甚至以后......

Thanks to @williammandra.com, I found that you need to create the primary key on a view manually. 感谢@ williammandra.com,我发现您需要手动在视图上创建主键。 But there's still another problem: for some reason when you load a value from the view Table2 and set it on the new record Table1 , then commit changes, it tries to insert a new record into Table2 , which obviously causes a PK violation. 但还有另一个问题:出于某种原因,当您从视图Table2加载一个值并将其设置在新记录Table1 ,然后提交更改时,它会尝试在Table2插入一条记录,这显然会导致PK违规。 Any idea why this happens, and how to get around it? 知道为什么会这样,以及如何绕过它?

Views don't have primary keys (without it the O/R designer can't create the relationship). 视图没有主键(没有它,O / R设计者无法创建关系)。 Your solution to use a view gets you halfway there.... The step you are missing is setting the "Primary Key" property to true in the O/R designer for the key field in the Table2 view. 您使用视图的解决方案会让您到达中途....您缺少的步骤是在O / R设计器中为Table2视图中的关键字段设置“Primary Key”属性为true。 You still have to create the association manually, but once you save the dbml the relationship will show up in the generated code. 您仍然需要手动创建关联,但是一旦保存dbml,关系将显示在生成的代码中。

You could create two dbml's, one for each db. 您可以创建两个dbml,每个db一个。 Then join the tables in your query: 然后加入查询中的表:

var tb1 = DataContext1.Table1
var tb2 = DataContext2.Table2

var result = (from t1 in tb1
              join t2 in tb2 on tb1.column equals tb2.column
              where ...
              select ...
             )

You could also set tb2 = to your view rather than another datacontext... 您也可以将tb2 =设置为您的视图而不是另一个datacontext ...

Assuming you can access one database from the other you can do this by manually editing the .dbml file. 假设您可以从另一个访问一个数据库,您可以通过手动编辑.dbml文件来完成此操作。

<Table Name="Table1.dbo.Table" Member="MemberObject"> 
<Table Name="Table2.dbo.Table" Member="MemberObject">

You might actually be able do this by looking at the properties of a table and changing the source. 实际上,您可以通过查看表的属性并更改源来实现此目的。

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

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