简体   繁体   English

实体框架:两种数据库模式,一种没有数据库上下文

[英]Entity Framework : two database schemas, one without db context

I've been trying to make a connection of two schemas in my application.我一直在尝试在我的应用程序中建立两个模式的连接。

I'll explain to you: I have an application that uses its own tables, so I created those with a migration and models and DbContext , then I hosted the tables in a SQL Server database that has other schemas from other applications and everything is ok.我将向您解释:我有一个使用自己的表的应用程序,因此我使用迁移和模型以及DbContext创建了这些表,然后我将这些表托管在 SQL Server 数据库中,该数据库具有来自其他应用程序的其他架构,一切正常. The single application connects and receive data.单个应用程序连接并接收数据。

But my application needs to be connected to one of the other schemas that I have in my SQL Server database, the other schema has the same connection string because they are in the same server.但是我的应用程序需要连接到我在 SQL Server 数据库中拥有的其他架构之一,另一个架构具有相同的连接字符串,因为它们在同一台服务器中。

I also wrote the 2 db context in startup.cs / ConfigureServices我还在startup.cs / ConfigureServices写了 2 db 上下文

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(_config.GetConnectionString("TrialOrdersConnectionString"), x => x.MigrationsHistoryTable("__MyMigrationsHistory", "trials")));
services.AddScoped(p => new ApplicationDbContext(p.GetService<DbContextOptions<ApplicationDbContext>>()));
//services.AddDbContext<AppDbContext_serie>(options => options.UseSqlServer(_config.GetConnectionString("Serie0ConnectionString")));

But of course, as I don't really have the applicationDbContext of the other schema is not recognized.但是当然,因为我真的没有其他架构的applicationDbContext不被识别。

I tried to repeat the application db context of the other schema to have the models and call them but in my migration it creates again the database :( and I don´t want that.我试图重复另一个模式的应用程序数据库上下文来拥有模型并调用它们,但在我的迁移中它再次创建了数据库 :( 我不想要那样。

I am using .Net Core and Angular.我正在使用 .Net Core 和 Angular。

For accessing the table in the database, but not in DbContext , you could try Query .要访问数据库中的表,但不在DbContext ,您可以尝试Query

Eg.例如。 Database has a table named PersonNotInDbContext which is not exist in DbContext .数据库有一个名为PersonNotInDbContext的表,它在DbContext不存在。

  • Table in database数据库中的表

    CREATE TABLE [dbo].[PersonNotInDbContext] ( [Id] INT NOT NULL, [Name] NCHAR (10) NULL, PRIMARY KEY CLUSTERED ([Id] ASC) );
  • Define a new model which mapped the return columns from sql query.定义一个映射 sql 查询返回列的新模型。

     public class TableNotInDbContext { public int Id { get; set; } public string Name { get; set; } }
  • Configure Query in DbContextDbContext配置Query

     public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { } public DbSet<TodoItem> TodoItem { get; set; } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Query<TableNotInDbContext>(); } }
  • Useage用途

     public async Task<IActionResult> DbQuery() { var result = await _context.Query<TableNotInDbContext>() .FromSql($"Select * From PersonNotInDbContext") .ToListAsync(); return Ok(result); }

    By Query Types , you will be able to run raw query from DbContext .通过 查询类型,您将能够从DbContext运行原始查询。

The way you configure the DI, you have 2 different instances configured for the type ApplicationDbContext .根据您配置 DI 的方式,您为ApplicationDbContext类型配置了 2 个不同的实例。 As far as I know the behavior for this will be:据我所知,这种行为将是:

  • injection for ApplicationDbContext will inject the first one ( AddDbContext definition). ApplicationDbContext注入将注入第一个( AddDbContext定义)。
  • injection for IEnumerable<ApplicationDbContext> will inject both contexts. IEnumerable<ApplicationDbContext>注入将注入两个上下文。

Note: Not sure if registering a type acts the same way as registering a interface in this case, could also be that the 2nd register overrides the first one.注意:不确定在这种情况下注册类型是否与注册接口的行为相同,也可能是第二个寄存器覆盖了第一个。

A workaround might be to create a 2nd DbContext class that inherits from ApplicationDbContext , then setup the 2nd class with the 2nd connectionstring.解决方法可能是创建从ApplicationDbContext继承的第二个 DbContext 类,然后使用第二个连接字符串设置第二个类。 But you have to know which one you want to use when using DI.但是在使用 DI 时,您必须知道要使用哪一个。

Otherwise a custom DbContextProvider might be a solution that lets you choice which DbContext you use.否则,自定义 DbContextProvider 可能是一种解决方案,可让您选择使用哪个 DbContext。 I posted an example code for one in on this question:我在这个问题上发布了一个示例代码:

Handling multiple connection strings in asp.net core web api which came as a parameter 在 asp.net core web api 中处理作为参数的多个连接字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM