3

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.

3 Answers 3

1

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.

3
  • 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, 2011 at 13:21
  • 1
    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. Mar 2, 2011 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, 2011 at 16:22
0

About the only way to do this is with Synonyms.

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

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

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

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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