如何让ABP找到我自己创建的数据库上下文?

huangapple go评论48阅读模式
英文:

How to make ABP find db context created by myself?

问题

I have used approach of several db contexts in ABP. I seperated ABP Identity module from other ABP modules. First of all I created new context for ABP Identity module using this articles - https://docs.abp.io/en/abp/latest/Entity-Framework-Core-Migrations#create-a-second-dbcontext and https://docs.abp.io/en/abp/latest/Entity-Framework-Core-Migrations#separating-host-tenant-database-schemas. As a result I have two db contexts.

First one:

[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class OMSBlazorDbContext :
    AbpDbContext<OMSBlazorDbContext>,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    public DbSet<CustomerDemographics> CustomerDemographics { get; set; }

    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

    public OMSBlazorDbContext(DbContextOptions<OMSBlazorDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<CustomerDemographics>(b =>
        {
            b.ToTable("CustomerDemographics");

            b.HasKey(x => x.CustomerTypeId);

            b.HasData(new CustomerDemographics() { CustomerTypeId = 1, CustomerDescription = "Lorem ipsum" });
        });
        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureOpenIddict();
        builder.ConfigureTenantManagement();
    }
}

Second one that contains identity:

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ConnectionStringName("AbpIdentity")]
public class OMSBlazorIdentityDbContext :
    AbpDbContext<OMSBlazorIdentityDbContext>,
    IIdentityDbContext
{
    public OMSBlazorIdentityDbContext(DbContextOptions<OMSBlazorIdentityDbContext> options) : base(options)
    {
    }

    public DbSet<IdentityUser> Users { get; set; }

    public DbSet<IdentityRole> Roles { get; set; }

    public DbSet<IdentityClaimType> ClaimTypes { get; set; }

    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }

    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }

    public DbSet<IdentityLinkUser> LinkUsers { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.SetMultiTenancySide(MultiTenancySides.Host);

        base.OnModelCreating(builder);

        builder.ConfigureIdentity();
        builder.ConfigureFeatureManagement();
        builder.ConfigureAuditLogging();
    }
}

When I start my Blazor app it shows me error that AbpFeatureGroups table not found but this table is situated in the second database (that one which can be accessed using OMSBlazorIdentityDbContext).

Why ABP cannot find AbpFeatureGroups table? Looks like ABP try to find this table in the first db context whileas it is in the second one that's my idea but in that case how to make it use second context. Correct me if I am wrong, please.

英文:

I have used approach of several db contexts in ABP. I seperated ABP Identity module from other ABP modules. First of all I created new context for ABP Identity module using this articles - https://docs.abp.io/en/abp/latest/Entity-Framework-Core-Migrations#create-a-second-dbcontext and https://docs.abp.io/en/abp/latest/Entity-Framework-Core-Migrations#separating-host-tenant-database-schemas. As a result I have two db contexts.

First one:

[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName(&quot;Default&quot;)]
public class OMSBlazorDbContext :
    AbpDbContext&lt;OMSBlazorDbContext&gt;,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    public DbSet&lt;CustomerDemographics&gt; CustomerDemographics { get; set; }

    // Tenant Management
    public DbSet&lt;Tenant&gt; Tenants { get; set; }
    public DbSet&lt;TenantConnectionString&gt; TenantConnectionStrings { get; set; }

    public OMSBlazorDbContext(DbContextOptions&lt;OMSBlazorDbContext&gt; options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity&lt;CustomerDemographics&gt;(b =&gt;
        {
            b.ToTable(&quot;CustomerDemographics&quot;);

            b.HasKey(x =&gt; x.CustomerTypeId);

            b.HasData(new CustomerDemographics() { CustomerTypeId = 1, CustomerDescription = &quot;Lorem ipsum&quot; });
        });
        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureOpenIddict();
        builder.ConfigureTenantManagement();
    }
}

Second one that contains identity:

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ConnectionStringName(&quot;AbpIdentity&quot;)]
public class OMSBlazorIdentityDbContext :
    AbpDbContext&lt;OMSBlazorIdentityDbContext&gt;,
    IIdentityDbContext
{
    public OMSBlazorIdentityDbContext(DbContextOptions&lt;OMSBlazorIdentityDbContext&gt; options) : base(options)
    {
    }

    public DbSet&lt;IdentityUser&gt; Users { get; set; }

    public DbSet&lt;IdentityRole&gt; Roles { get; set; }

    public DbSet&lt;IdentityClaimType&gt; ClaimTypes { get; set; }

    public DbSet&lt;OrganizationUnit&gt; OrganizationUnits { get; set; }

    public DbSet&lt;IdentitySecurityLog&gt; SecurityLogs { get; set; }

    public DbSet&lt;IdentityLinkUser&gt; LinkUsers { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.SetMultiTenancySide(MultiTenancySides.Host);

        base.OnModelCreating(builder);

        builder.ConfigureIdentity();
        builder.ConfigureFeatureManagement();
        builder.ConfigureAuditLogging();
    }
}

When I start my Blazor app it shows me error that AbpFeatureGroups table not found but this table is situated in the second database (that one which can be accessed using OMSBlazorIdentityDbContext).

Why ABP cannot find AbpFeatureGroups table? Looks like ABP try to find this table in the first db context whileas it is in the second one that's my idea but in that case how to make it use second context. Correct me if I am wrong, please.

答案1

得分: 0

添加到 appsettings.jsonConnectionString 部分的解决方案是添加名为 "AbpFeatureManagement" 的连接字符串,并将其设置为指向包含 AbpFeatureGroups 表的数据库的连接字符串。希望对您有所帮助。

英文:

The soultion that worked was adding to the appsettings.json's ConnectionString section connection string with name "AbpFeatureManagement" and setting it to the connection string which lead to database where AbpFeatureGroups table is. Hope it helps

huangapple
  • 本文由 发表于 2023年5月6日 17:53:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定