Mike Devenney
Mike Devenney

Reputation: 1845

Access related data from two databases through a single EF 6 data layer

My team would like to separate User data from our core application data. We're using SQL Server 2014 and EF 6.x Code First. If we create a Users database and an Application database is there any way to tell EF about a relationship between the User object and and Order object? Assuming the Orders are stored in the application database and the users are stored in the Users database. We know that we could instantiate the User from the Users database and then query the Application database for a list of orders, but what we're looking for is a solution that functions as if the tables were both in the same database, such as when a User is retrieved its list of Orders is available without a second call.

Basically, we're looking for a way to map entities to a specific context and then instantiate each context pointing to its respective database and let EF sort out the calls.

Upvotes: 0

Views: 37

Answers (1)

Vlad274
Vlad274

Reputation: 6844

Yes, but you need to do some configuration one the database servers.

Your Application database can define a Synonym to the table in the Users database, which you can then map an entity to as if it were a normal table in the Application DB.

As far as Entity Framework is concerned, it uses the same DbContext pointing to your Application database, but it can pull information from your Users database.

One downside with this is that your Application database needs to be setup to point at a specific Users database.

Upvotes: 1

Related Questions