[英]Pass DbContextOptions<dbContext> to base context
I need to set up a one-to-one relationship between two tables that reside in two separate databases: db1.Asset and db2.AssetExtra . 我需要在两个单独的数据库中的两个表之间建立一对一的关系: db1.Asset和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 .
该关系在数据库中不存在,因此我需要使用Fluent Api进行设置,这是我根据文档进行的 。
BaseContext.OnModelCreating(ModelBuilder modelBuilder)
contains this (other things omitted for brevity): BaseContext.OnModelCreating(ModelBuilder modelBuilder)
包含以下内容(为简便起见,省略了其他内容):
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: Asset.cs
具有AssetExtra
属性, AssetExtra.cs
具有Asset
和AssetId
属性:
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: 在
Startup.cs
,每个上下文都会注入其连接字符串:
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: 按照此答案中的示例,我试图创建一个基本上下文,该上下文包含所有DbSet并设置两个数据库的所有模型关系:
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: 当我尝试使用
.Include()
将AssetExtra包含在Asset中时,出现运行时错误:
var assets = this.db1Context.Assets.Where(m => assetIds.Contains(m.AssetId))
.Include(a => a.AssetExtra)
.ToList();
SqlException: Invalid object name Asset.AssetExtra. SqlException:无效的对象名称Asset.AssetExtra。
Which I assume is because the AssetExtra relationship doesn't actually exist in the database? 我认为是因为AssetExtra关系在数据库中实际上不存在? How can I get this working?
我该如何工作?
I'm afraid you cannot do this using Entity Framework. 恐怕您无法使用Entity Framework进行此操作。 It is designed to be used one context - one database.
它旨在用于一种上下文-一种数据库。 Relationships must be configured as part of the model which means they must point to valid objects in the database you are connecting to.
关系必须配置为模型的一部分,这意味着它们必须指向您要连接到的数据库中的有效对象。
What you are looking for is: 您正在寻找的是:
I think you need to take a deep look at what you are trying to achieve and how. 我认为您需要深入了解您要实现的目标以及如何实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.