Search code examples
sql-server-2008entity-framework-4sapb1

Entity Framework expose several databases as a whole


I have three databases with exactly the same schema (SAP Business One databases). In these databases I have an item masters table connected to a warehouse stock table via the item code.

Can I have just one Entity framework model that has only one item master object and one warehouse stocks object which draws data from the 3 databases?

The items are the same in the three databases but they have different warehouse codes.

I don't know if I have made myself clear.


Solution

  • If you want single EF model which will simultaneously load data from three databases then answer is no. If you want single EF model which can be used for all three databases the answer is yes but all your databases must use same database provider (server) and must have exactly the same schema of mapped tables.

    The whole magic in this case is in connection string which can connect only to single database and cross database calls are not allowed.

    If you need the first scenario you can try to hide unions and cross database queries in views and map those views in your model. This have two disadvantages:

    • Relation between views are not allowed in SQL Server but you can create the relation in EF model
    • Views are read only in EF model. If you want to modify data the best way is mapping stored procedures which will do that.