Question

I have 2 databases, each with a corresponding EntityFramework DB Context and I am using Entity Framework 5, Code-First.

I have tables resembling the following:

Table     : Foo (resides in database 1)
-- Column : Id (is primary key)
-- Column : ParentId (is foreign key into Foo)
-- Column : BarId (is foreign key into Bar)

Table     : FooBar (resides in database 1)
-- Column : Id (is primary key)
-- Column : FooId (is foreign key into Foo)
-- Column : BarId

Table     : Bar (resides in database 2)
-- Column : Id (primary key)

Then, I have entities Foo, FooBar in Database1DbContext and Bar in Database2DbContext.

Foo has navigation properties Parent (of type Foo), Children (of type ICollection<Foo>) and Bar (of type Bar). FooBar has navigation properties Foo (of type Foo) and Bar (of type Bar).

Now, what I want is to be able to .Include(foo => foo.Bar), such that Bar is loaded up for all the Parent and Children of a Foo as well.

I hope I have not been too confusing.

PS: I know there is going to be an issue setting up the Bar navigation property since it is on a different database. I saw suggestions in some related answers suggesting either views or synonyms. Any answers that also take into account that aspect of my situation will be greatly appreciated.

Was it helpful?

Solution

It is not possible. You cannot make cross context queries or cross context eager / lazy loading. You must load Bar instances yourselves for all required Foo and FooBars.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top