所以我有来自 2 个数据库的 2 个实体。'tblApp' 来自 Db1,'tblGroup' 来自 Db2。如何从 2 个数据库中建立 2 个表的多对多关系
目前我有
public class EFDbContextDb1 : DbContext
{
public DbSet<Group> tblGroup { get; set; }
// the problem is 'tblApp' is in the Db2 database
// so I guess I should do something here
public DbSet<App> tblApp{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Group>().ToTable("tblGroup");
modelBuilder.Entity<App>()
.HasMany(g => g.tblGroup)
.WithMany(a => a.tblApp)
.Map(m =>
{
m.MapLeftKey("AppID");
m.MapRightKey("GroupID");
m.ToTable("AppGroup");
});
}
}
public class EFDbContextDb2 : DbContext
{
public DbSet<App> tblsApp{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<App>().ToTable("tblApp");
base.OnModelCreating(modelBuilder);
}
}
与“Db1”和“Db2”连接的连接字符串
<connectionStrings>
<add name="EFDbContextDb1" providerName="System.Data.SqlClient" connectionString="Data Source=Db1;Initial Catalog=Security;Integrated Security=true;" />
<add name="EFDbContextDb2" providerName="System.Data.SqlClient" connectionString="Data Source=Db2;Initial Catalog=ServerStatus;Integrated Security=true;" />
</connectionStrings>