【发布时间】:2018-10-09 17:11:47
【问题描述】:
我在尝试从代表不同数据库的 2 个 DbContext 中加入 2 个表时遇到问题。
我正在使用 Fluent API 在 2 个表之间创建外键关系。 以下是 Dbcontext 配置和模型。
public class DbContextDemo1: DbContext
{
public DbSet<Agency> Agencies { get; set; }
public DbContextDemo1(DbContextOptions<DbContextDemo1> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("db1")
.Entity<Agency>()
.ToTable("agencies")
.HasKey(agency => agency.Id)
.HasOne(agency => agency.AgencyApp)
.WithOne(app => app.Agency)
.HasForeignKey<Agency>(agency => agency.Id);
}
}
public class DbContextDemo2: DbContext
{
public DbSet<AgencyApp> AgencyApps { get; set; }
public DbContextDemo2(DbContextOptions<DbContextDemo2> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("db2")
.Entity<AgencyApp>()
.ToTable("agenciesapps")
.HasKey(app => app .Id)
.HasOne(app=> app.Agency)
.WithOne(agency => agency.AgencyApp)
.HasForeignKey<AgencyApp>(app=> app.AgencyId);
}
}
以下是模型:
public class Agency
{
public Guid Id { get; set; }
public AgencyApp AgencyApp { get; set; }
}
public class AgencyApp
{
public Guid Id { get; set; }
public Guid AgencyId { get; set; }
public Agency Agency { get; set; }
}
现在,当我尝试与 AgencyApp 一起获取 Agencies 数据时。
var result = _dbContextDemo1.Agencies.Include(agency => agency.AgencyApplication)
会报错
“表 'db2.agenciesapps' 不存在”。
我可以在服务器控制台中看到它正在这两个表之间进行内部连接。
我们将不胜感激。谢谢
【问题讨论】:
标签: asp.net-core ef-fluent-api ef-core-2.0