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

I am a little new to Linq and I could not find documentation that would help me with something like a rather trivial problem, so your help would be greatly appreciated!

I have a table Table1in a database DB1that has a "pseudo" foreign key Table2IDin a table Table2in a database DB2on the same server. Pseudo, because obviously I can't have the actual FK spanning two databases.

Now I play with the O / R designer, and I like how all relationships are created when I cast database objects to the constructor ... very cool! And I want my object to Table1be related to Table2, just as it has a relationship with all the "real" objects associated with the foreign key, in DB1. But I can’t bring Table2db to my diagram because it is in the wrong DB.

To synthesize this, I tried to create a view Table2in DB1that which is simple select * from DB2..Table2. Yeah, now I can delete the object Table2in my diagram. I can even establish relationships between parents and children between Table1and Table2. But when I look at the generated code, Table1it still has nothing to do with Table2, which I find most puzzled.

Am I missing a step somewhere? Is there a better / recommended way to do this?

Thanks!


Further...

In accordance with what one person suggested, I tried to fill out a partial class with Table1all the methods necessary to access Table2, by copying all the structures for the linked object within the same database.

, , :

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.

, , Linq , . ...: (


... ...

@williammandra.com, , . : - , Table2 Table1, , new Table2, , , PK. , , ?

+5
3

( O/R ). .... " " true O/R Table2. , dbml, .

+4

dbml, db. :

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

tb2 = , datacontext...

0

, , , .dbml.

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

In fact, you can do this by looking at the properties of the table and changing the source code.

0
source

All Articles