Search code examples
c#asp.net-core.net-coreentity-framework-coreef-core-5.0

EF Core and Multiple Databases


I have a legacy system with three databases

  1. Vendor
  2. CustomCode
  3. LogData

Vendor contains control and log data from our Vendors app.

CustomCode contains lots of views and stored procedures that joins to Vendor and LogData

LogData contains results from our CustomCode processes. eg: Daily/Weekly/Monthly summaries and results.

I'm writing a website that will plot data on a map. The list of units is from a view in CustomCode. The Summary record is from LogData, and the individual log points are retrieved from Vendor by a stored proc in CustomCode.

I started with a DbContext for CustomCode, but can't seem to Navigate to properties in a 2nd DbContext to LogData

Can I link navigation properties between objects in different contexts?

Can I have once context with multiple databases connected ?

Please note, this is nothing to do with multi-tenant or multi-schema


Solution

  • Can I link navigation properties between objects in different contexts?

    No.

    Can I have one context with multiple databases connected?

    No.

    Suggestion:

    If the databases can communicate to each other (ie on same server), which appears to be already done since

    CustomCode contains lots of views and stored procedures that joins to Vendor and LogData

    then create a stored procedure to perform the desired queries (which can join tables from separate databases).

    From there you should be able to expose and execute the procedure from Entity Framework to perform the desired functionality.

    This would avoid have multiple contexts and trying to join the data in memory, which can have adverse effects if the data set is large.