Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if anyone had the same need and why (if possible).

My problem is: I can't create a model in EF using two tables from different DBs without some weird workarounds. For my scope that is very much necessary.

Did any of you ever needed to do this? What did you do? And, like me, do you think MS should add this feature?

Thank you all.

share|improve this question
up vote 2 down vote accepted

I think the general paradigm is that a single EF model goes against a single database. You can always create multiple models in a project. If you need to do joins or in other ways combine the data between the two models, just do it with LINQ to Objects once you get the data into memory.

Alternatively, you can look into technologies which create database-to-database links right within the DBMS. I know that both SQL Server and Oracle have solutions for this; I'm sure other major DMBS products have similar features. Using this, your EF model would only connect to one database, but that first database could have views that use database links to connect to the second database and combine the data. It will almost definitely more efficient to combine the data at the DBMS level versus at the EF level.

share|improve this answer
Ok, I agree. but don't you think it should be easy for them to add this feature? way easier then us having to do these workarounds. IMHO – eestein Mar 2 '11 at 13:21
1) Every feature requires testing and years of ongoing support. 2) Just because you can add it doesn't mean you should. If the recommended approach is to not do it this way, then not supporting it is probably for the best. – RationalGeek Mar 2 '11 at 15:53
It's true, but due to the amount of people talking about this need I guess they should really consider adding it. @gustavo is just one in many (including me). Regards. – eestein Mar 2 '11 at 16:22

About the only way to do this is with Synonyms.

In SQL Server, if I were to use the MSDN example

    USE tempdb;
-- Create a synonym for the Product table in AdventureWorks2012.
FOR AdventureWorks2012.Production.Product;

-- Query the Product table by using the synonym.
USE tempdb;
SELECT ProductID, Name 
FROM MyProduct
WHERE ProductID < 5;

Then EF would be able to see MyProduct if it were aimed at tempdb.

share|improve this answer

I had a similar scenario recently for a Entity Framework Database First project. I had to create an Entity from data spread across multiple tables, in-fact some of the data came from another database (but on the same Db server).

To get around this I created stored procedures for the required data and imported the procedure to Entity Framework using the Function Imports in Model Browser.

Now when I use that Entity, it calls a stored procedure in the background to retrieve the data. Your question about Microsoft adding this as a feature is answered, they already have. That is what the Function Imports are for.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.