ASP.NET Boilerplate 多个数据库和 DbContexts [英] ASP.NET Boilerplate multiple databases and DbContexts

查看:28
本文介绍了ASP.NET Boilerplate 多个数据库和 DbContexts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ASP.NET Boilerplate 一次连接到两个数据库.我按照这个例子:https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextEfCoreDemo

问题是只有第一个上下文才会有所有的 Abp 表.第二个上下文是一个现有的数据库,其中没有任何 Abp 表.

当我启动网络应用程序时,我得到:

System.Data.SqlClient.SqlException: '无效的对象名称'AbpLanguages'.

很明显,它正在第二个上下文中寻找 Abp* 表,但它们不存在(而且它们不应该存在).那么,拥有多个上下文的正确方法是什么,这样它就不会在第二个上下文中查找 Abp* 表?

更新

我通过修改从模板生成的干净的 ASP.NET Boilerplate 项目创建了一个示例应用程序.

我已经测试了一个从模板生成的项目,该项目仅适用于 API,并且运行良好.但是当 API 和前端在生成项目时结合使用时,我会遇到这些问题,其中 Abp* 表是在其他上下文中查找的.

Dropbox:https://www.dropbox.com/s/19gj8ms0jwz500k/4.0.1.zip?dl=0
Github:https://github.com/FrikkinLazer/MultipleContext.git

解决方案

<块引用>

//使用 Abp.Zero.EntityFrameworkCore;公共部分类 TEST1Context : AbpZeroDbContext{//...}

Abp.Zero.EntityFrameworkCoreAbpZeroDbContext 定义模块零表的DbSets.

<块引用>

第二个上下文是一个现有的数据库,上面没有任何 Abp 表.

那么,拥有多个上下文的正确方法是什么,这样它就不会在第二个上下文中查找 Abp* 表?

对于第二个上下文,子类 Abp.EntityFrameworkCoreAbpDbContext.

I want to connect to two databases at once using ASP.NET Boilerplate. I followed this example: https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextEfCoreDemo

The problem is that only the first context will have all the Abp tables on it. The second context is an existing database, which has none of the Abp tables on it.

When I start up the web app, I get this:

System.Data.SqlClient.SqlException: 'Invalid object name 'AbpLanguages'.'

So obviously it is looking for Abp* tables on the second context, but they don't exist there (and they should not). So, what is the correct way to have more than one context, so that it doesn't look for Abp* tables on the second context?

Update

I have created an example application by modifying a clean ASP.NET Boilerplate project generated from a template.

I have tested with a project generated from the template which is API-only, and there it works perfectly. But when API and frontend are combined when generating the project, I am getting these issues where the Abp* tables are looked for on additional contexts.

Dropbox: https://www.dropbox.com/s/19gj8ms0jwz500k/4.0.1.zip?dl=0
Github: https://github.com/FrikkinLazer/MultipleContext.git

解决方案

// using Abp.Zero.EntityFrameworkCore;

public partial class TEST1Context : AbpZeroDbContext<Tenant, Role, User, TEST1Context>
{
    // ...
}

Abp.Zero.EntityFrameworkCore's AbpZeroDbContext defines the DbSets for Module Zero tables.

the second context is an existing database, that have none of the Abp tables on it.

So, what is the correct way to have more than one context, so that it doesnt look for Abp* tables on the second context?

For the second context, subclass Abp.EntityFrameworkCore's AbpDbContext.

这篇关于ASP.NET Boilerplate 多个数据库和 DbContexts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆