Open Closed

EfCoreEntityExtensionMappings Only for Tenants Database #5406


User avatar
0
pablo@ccalp.net created
  • ABP Framework version: v7.2
  • UI type: Angular
  • DB provider: EF Core

Hello, I need to extend the IdentityUser just for the tenants, I'm using the EfCoreEntityExtensionMappings class but when I run the migration I get an error because I assume that one of the foreign keys to a table that only exists in the tenant databases is not present in the host. How can I just extend the IdentityUser just for the tenants?

Extension mapping code here:

using CompuCare.Entities;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Threading;
namespace CompuCare.EntityFrameworkCore;
public static class CompuCareEfCoreEntityExtensionMappings
{
    private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
    public static void Configure()
    {
        CompuCareGlobalFeatureConfigurator.Configure();
        CompuCareModuleExtensionConfigurator.Configure();

        OneTimeRunner.Run(() =>
        {
            ObjectExtensionManager.Instance
                .MapEfCoreProperty<IdentityUser, string>(
                    "SSN",
                    (entityBuilder, propertyBuilder) =>
                    {
                        propertyBuilder.HasMaxLength(9);
                    }
                )
                .MapEfCoreProperty<IdentityUser, int?>(
                    "DoctorId",
                    (entityBuilder, propertyBuilder) =>
                    {
                        propertyBuilder.IsRequired(false);
                        entityBuilder.HasOne(typeof(Entities.Doctor))
                            .WithMany()
                            .IsRequired(false);
                    }
                )
                .MapEfCoreProperty<IdentityUser, int?>(
                    "TypeId",
                    (entityBuilder, propertyBuilder) =>
                    {
                        propertyBuilder.IsRequired(false);
                        entityBuilder.HasOne(typeof(Entities.Type))
                            .WithMany()
                            .IsRequired(false);
                    }
                );
        });
    }
}

Migration that was created:

using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CompuCare.TenantMigrations
{
    /// <inheritdoc />
    public partial class IdentityUserExtraProperties : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "DoctorId",
                table: "AbpUsers",
                type: "int",
                nullable: true);
            migrationBuilder.AddColumn<string>(
                name: "SSN",
                table: "AbpUsers",
                type: "nvarchar(9)",
                maxLength: 9,
                nullable: true);
            migrationBuilder.AddColumn<int>(
                name: "TypeId",
                table: "AbpUsers",
                type: "int",
                nullable: true);
            migrationBuilder.CreateIndex(
                name: "IX_AbpUsers_DoctorId",
                table: "AbpUsers",
                column: "DoctorId");
            migrationBuilder.CreateIndex(
                name: "IX_AbpUsers_TypeId",
                table: "AbpUsers",
                column: "TypeId");
            migrationBuilder.AddForeignKey(
                name: "FK_AbpUsers_Doctors_DoctorId",
                table: "AbpUsers",
                column: "DoctorId",
                principalTable: "Doctors",
                principalColumn: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_AbpUsers_Types_TypeId",
                table: "AbpUsers",
                column: "TypeId",
                principalTable: "Types",
                principalColumn: "Id");
        }
        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_AbpUsers_Doctors_DoctorId",
                table: "AbpUsers");
            migrationBuilder.DropForeignKey(
                name: "FK_AbpUsers_Types_TypeId",
                table: "AbpUsers");
            migrationBuilder.DropIndex(
                name: "IX_AbpUsers_DoctorId",
                table: "AbpUsers");
            migrationBuilder.DropIndex(
                name: "IX_AbpUsers_TypeId",
                table: "AbpUsers");
            migrationBuilder.DropColumn(
                name: "DoctorId",
                table: "AbpUsers");
            migrationBuilder.DropColumn(
                name: "SSN",
                table: "AbpUsers");
            migrationBuilder.DropColumn(
                name: "TypeId",
                table: "AbpUsers");
        }
    }
}

7 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share a simple project? liming.ma@volosoft.com

  • User Avatar
    0
    pablo@ccalp.net created

    I cannot share my project and will take too long to create a sample with the right conditions. My use case is very simple: Is it possible to tell the ObjectExtensionManager that the mappings are just for the Tenant and not for the host? Just like when creating the model builder.SetMultiTenancySide(MultiTenancySides.Tenant);

    Or is there any other way to add the additional columns to the AbpUsers table?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The ObjectExtensionManager work on globally, Doesn't support the MultiTenancySide now.

  • User Avatar
    0
    pablo@ccalp.net created

    Ok, so if I wanted to add a foreign key in the AbpUsers to another table, that table must exist in the Host and the Tenant databases, right? BTW, I have the host and tenant DbContexts separated.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Yes, because AbpUsers table exists in both the host and tenant database.

  • User Avatar
    0
    pablo@ccalp.net created

    One more questions: What do I have to do to merge the host and the tenant DbContext, so basically not to separate the host from the tenant databases.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can create a project which not separate dbcontext and copy the code.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11