Is there a way to use Entity Framework with more than one database? Even further make relationship between them tables? I have multiple projects which use each other tables somehow, I want to use them with separate databases for some reasons, so don't suggest to merge them in one database with schemas
2 Answers
You can if you are using Sql Server 2005 and above by using Synonyms.
For example: Say you have three sql server databases. You could choose one to be the "main" database and create synonyms in the main database for the tables that are in the other two databases. Then you could use the synonym names when creating the entity models for the tables that are not in the main database.
4 Comments
Serjik
I've created synonyms, but they not appear I model any objects (views, tables, procedures) so how could I import this in my model.edmx ?
Colin
Could you create Views in the main database that are based on the external tables? Then Insert and Update via stored procedures?
Tom Brothers
I can think of two ways to get this to work with an edmx but I wouldn’t suggest either of them. The first way would be to manually edit the edmx to include the synonyms that you created. If you do this you’ll want to avoid using the designer because it will overwrite your changes (which defeats the purpose of using edmx). The second way would be to use a View to see the data and Stored Procedures to manipulate the data (as Colin mentioned). I think this option would be annoying because it would be less convenient to work with the data.
Tom Brothers
Personally, I’d suggest using the “code first” approach which is what my original answer had assumed. You could use the Entity Framework Power Tools to code gen all your entities and then you would just need to change the table names to the synonyms.