简体   繁体   English

传递DbContextOptions <dbContext> 根据上下文

[英]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.Assetdb2.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具有AssetAssetId属性:

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: 您正在寻找的是:

  1. Not a valid constraint, since constraints are stored in the database and can only reference items that live in the database schema. 这不是有效的约束,因为约束存储在数据库中,并且只能引用数据库模式中存在的项目。 There's a reason you mention the relationship doesn't exist and you are looking to do this through code, isn't there ? 有一个原因提到您的关系不存在,并且您正在寻找通过代码实现的关系, 不是吗? I would suggest you to read this Q&A: Add Foreign Key relationship between two Databases . 我建议您阅读以下问答: 在两个数据库之间添加外键关系
  2. Not an actually logical relationship even if it was stored in a single database, since one-to-one required relationships are impossible to model (think what entity inserts the ID of the other entity). 即使它存储在单个数据库中,也不是真正的逻辑关系,因为一对一的必需关系无法建模(请考虑哪个实体插入另一个实体的ID)。 This Q&A can provide more details: Entity Framework Code First One-to-One Required-Required Relationship 此问答可以提供更多详细信息: 实体框架代码首先一对一必填

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.

相关问题 在EF Core中创建DbContext实例时如何传递DbContextOptions值 - How to pass DbContextOptions value while creating DbContext instance in EF Core 通过使用DbContextOptions的配置获取DbContext - Getting DbContext via Configuration with DbContextOptions 从 DbContext 中提取 EF Core DbContextOptions - Extract EF Core DbContextOptions from DbContext 何时在 DbContext 构造函数与 OnConfiguring 中提供 DbContextOptions? - when to provide DbContextOptions in DbContext constructor vs OnConfiguring? 将上下文传递给在基类中声明的方法 - Pass context to a method that is declared in the base class 在asp.core中我的dbcontext问题。 不知道如何使用DbContextOptions对象 - issue with my dbcontext in asp.core. Dont know how to use DbContextOptions object 没有为此 DbContext 配置数据库提供程序,即使将 DbContextOptions 传递给构造函数 - No database provider has been configured for this DbContext, even when passing DbContextOptions to constructor C# DbContext 问题:参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions” - C# Problem with DbContext: Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions' 将过滤器参数传递给 DBContext - Pass Filter Parameter To DBContext 仅使用一个上下文使用DbContext - Using DbContext with only one context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM