Say I have two DbContext, one containing breeds and one containing dogs.
public class Dog {
public Breed Breed { get; set; }
public Guid Id { get; set; }
public string Name { get; set; }
}
public class Breed {
public Guid Id { get; set; }
public string Name { get; set; }
}
public class MainUnitOfWork : DbContext
{
public MainUnitOfWork(): base("MainDb") { }
public IDbSet<Breed> Breeds { get; set; }
}
public class NextUnitOfWork : DbContext
{
public NextUnitOfWork() : base("NextDb") { }
public IDbSet<Dog> Dogs { get; set; }
}
Now I try to insert a new dog.
var next = new NextUnitOfWork();
var dog = new Dog
{ Id = Guid.NewGuid(), Name = "Fido",
Breed = new Breed { Id = Guid.NewGuid(), Name = "Rotweiler" } };
next.Dogs.Add(dog);
next.SaveChanges();
Now I get two tables in NextDb, one for Dogs and one for Breeds. I hoped EF would figure out that the Breed really belonged to the MainDb since it’s declared in the MainUnitOfWork. Well, how do I proceed to bend EF to do what I want?
Even if EF would find a way to do it, it would be impossible to represent that in a relational database because there is no way to define a foreign key to a table in another database. At least not in mainstream relational database servers.
Dog table needs a foreign key to Breed to represent the many-to-one relationship.