I need to set up a one-to-one relationship between two tables that reside in two separate databases: db1.Asset and db2.AssetExtra. This relationship does not exist in the database, so I need to set it up using the Fluent Api, which I did according to the documentation.
BaseContext.OnModelCreating(ModelBuilder modelBuilder)
contains this (other things omitted for brevity):
modelBuilder.Entity<Asset>()
.HasOne(a => a.AssetExtra)
.WithOne(e => e.Asset)
.HasForeignKey<AssetExtra>(e => e.AssetId);
Asset.cs
has an AssetExtra
property, and AssetExtra.cs
has an Asset
and an AssetId
property:
public partial class AssetExtra
{
public int AssetId { get; set; }
public Asset Asset { get; set; }
}
public partial class Asset
{
public int AssetId { get; set; }
public int AssetExtra { get; set; }
}
In Startup.cs
, each context is injected with its connection string:
services.AddDbContext<db1Context>(options => options.UseSqlServer(this.Configuration.GetConnectionString("db1")));
services.AddDbContext<db2Context>(options => options.UseSqlServer(this.Configuration.GetConnectionString("db2")));
Following the example in this answer, I'm trying to create one base context which holds all of the DbSets and sets up all of the model relationships for both databases:
public class BaseContext : DbContext
{
public BaseContext(DbContextOptions<db1Context> options)
: base(options)
{
}
public BaseContext(DbContextOptions<db2Context> options)
: base(options)
{
}
}
public class db1Context : BaseContext
{
public db1Context()
{
}
public db1Context(DbContextOptions<db1Context> options)
: base(options)
{
}
}
public class db2Context : BaseContext
{
public db2Context()
{
}
public db2Context(DbContextOptions<db2Context> options)
: base(options)
{
}
}
When I try to use .Include()
to include AssetExtra with Asset, I get a runtime error:
var assets = this.db1Context.Assets.Where(m => assetIds.Contains(m.AssetId))
.Include(a => a.AssetExtra)
.ToList();
SqlException: Invalid object name Asset.AssetExtra.
Which I assume is because the AssetExtra relationship doesn't actually exist in the database? How can I get this working?