Search code examples
asp.net-mvcentity-frameworkcode-firstrelationshipmultiple-databases

Entity Framework Code First - Relationship between multiple databases


I would like to know if it is possible to map relationship between two different databases using Code First. For example i want to deploy different websites with the same database structure(same system). But i want one Master Database to have tables that all systems will share access.

Example: Table Clients in Master Database; Table ClientContacs in individual specific sytem's database;

Is it possible to map using Code First and multiple DbContext? If it is not, i really would appreciate suggestions on how to achiev that! Thanks!


Solution

  • Yes, it's possible, but not very convenient.

    Here's a description of doing it with EDMX Files:

    http://rachel53461.wordpress.com/2011/05/22/tricking-ef-to-span-multiple-databases/

    It's a bit easier with code first, since you need only reference the table names. However, you can't get EF to create this automatically. You have to create the synonym first and then treat the synonym as a normal table.

    Obviously, this only works in a database that supports Synonyms... SQL Server 2008+ should be fine.

    But, this is really treating it as a single database with "links" to the other database as tables... you can't treat it as two separate databases explicitly.