MetaGuru
MetaGuru

Reputation: 43873

Tables from two different databases in a DBML?

After dragging two tables in from one database, I switch to another and drag a table in. Now I get a message if I want to replace the connection string with the new one. I want tables from multiple databases in one DBML. Is this possible?

Upvotes: 5

Views: 3073

Answers (4)

user386167
user386167

Reputation:

We can also create a view that queries the table in the other database. We can select, insert and update this view, which will affect the table in the other database as well.

Upvotes: 0

zax
zax

Reputation: 956

It is entirely possible to reference multiple databases within the same DBML, PROVIDED those databases reside on the same SQL Server.

In Visual Studio, right-click on the DBML, click "Open with..." , and select XML (Text) Editor with Encoding.

You will see your first table that you dragged in looks like this:

<Table Name="dbo.MyTable1fromMyDatabase1" Member="MyTable1fromMyDatabase1">

For your tables from other databases you wish to add, enter them like this:

<Table Name="MyDatabase2.dbo.MyTable1fromMyDatabase2" Member="MyTable1fromMyDatabase2">

This will work assuming the same login works for both databases, and your LINQ expressions can now query across both databases!

Upvotes: 5

Michael Brown
Michael Brown

Reputation: 9153

Another option is to create a server link on on database that points to the other and make aliases to the remote tables from the "local" DB. I believe then you'd be able to reference them as if they were all in the same database.

Upvotes: 0

Josh E
Josh E

Reputation: 7432

I don't believe that what you're looking for is possible, since the DataContext would then not have any easy way of resolving results from two separate databases.

If you're looking to create domain objects from two separate databases, then your best bet would be to have two separate DBML's, then use a bridge (GOF) or some other related design pattern to instantiate your domain objects.

Upvotes: 3

Related Questions