EntityFramework: One DB Context for Multiple Databases

by Joe572Thomas   Last Updated June 11, 2019 06:26 AM

We have multiple databases in our system, around 15+. Is there a way to query multiple databases with one DBContext (combining all databases) using Entity Framework?

Example databases, CustomerDatabase, ShippingDatabase, Orders

var test= (from c in dbContext.Customer
                 join o in dbContext.Orders on c.CustomerId equals o.CustomerId 
                 join s in dbContext.Shipping on o.OrderId equals s.OrderId

Answers 1

I don't believe there's a 'nice' way to do this like you're looking for, but there are some possible approaches in the below question that might help you, including using database views which is an approach I've taken in the past.

Cross database querying in EF

Rob Bransby
Rob Bransby
June 11, 2019 06:25 AM

Related Questions

Scaffold-DbContext doesn't produce models

Updated December 07, 2017 22:26 PM

Same DbContext, Several databases using EF Core

Updated April 12, 2018 09:26 AM