说我有两个DbContext,一个包含品种,另一个包含狗。在两个DbContext之间共享
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; }
}
现在我试着插入一只新的狗。
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();
现在我在NextDb中得到两个表格,一个用于狗,一个用于品种。我希望EF能够发现,自从在MainUnitOfWork中声明后,该品种确实属于MainDb。那么,我如何继续弯曲EF来做我想做的事?