Open Closed

Cannot use table 'AbpUsers' for entity type 'IdentityUser' since it is being used for entity type 'AppUser' #2894


User avatar
0
abllyboy created
  • ABP Framework version: v5.2.0

  • UI type: MVCr

  • DB provider: EF Core

  • Tiered (MVC) or Identity Server Separated (Angular): yes

  • Exception message and stack trace:

  • Steps to reproduce the issue:"

  • step1 I user abp suite generate a project ,like below

  • step2 Add AppUser in the domain project,and add my own properties "IdCard",like below

  • ` public class AppUser : FullAuditedAggregateRoot

      /* These properties are shared with the IdentityUser entity of the Identity module.
       * Do not change these properties through this class. Instead, use Identity module
       * services (like IdentityUserManager) to change them.
       * So, this properties are designed as read only!
       */
    
      public virtual Guid? TenantId { get; private set; }
    
      public virtual string UserName { get; private set; }
    
      public virtual string Name { get; private set; }
    
      public virtual string Surname { get; private set; }
    
      public virtual string Email { get; private set; }
    
      public virtual bool EmailConfirmed { get; private set; }
    
      public virtual string PhoneNumber { get; private set; }
    
      public virtual bool PhoneNumberConfirmed { get; private set; }
    
      #endregion
    
      /* Add your own properties here. Example:
       *
       * public string MyProperty { get; set; }
       *
       * If you add a property and using the EF Core, remember these;
       *
       * 1. Update InfrastructureDbContext.OnModelCreating
       * to configure the mapping for your new property
       * 2. Update InfrastructureDbContextEfCoreEntityExtensionMappings to extend the IdentityUser entity
       * and add your new property to the migration.
       * 3. Use the Add-Migration to add a new database migration.
       * 4. Run the .DbMigrator project (or use the Update-Database command) to apply
       * schema change to the database.
       */
      public string IdCard { get; set; }
      private AppUser()
      {
    
      }
    

    }`

  • step 3 In the EntityFrameworkCore project's DemoDbContext.cs file,I added AppUser's DbSet,and configed AppUser mapping, like below

  • `public class DemoDbContext : AbpDbContext

    #region Entities from the modules

    /* Notice: We only implemented IIdentityProDbContext and ISaasDbContext

    • and replaced them for this DbContext. This allows you to perform JOIN
    • queries for the entities of these modules over the repositories easily. You
    • typically don't need that for other modules. But, if you need, you can
    • implement the DbContext interface of the needed module and use ReplaceDbContext
    • attribute just like IIdentityProDbContext and ISaasDbContext.
    • More info: Replacing a DbContext of a module ensures that the related module
    • uses this DbContext on runtime. Otherwise, it will use its own DbContext class. */

    // Identity public DbSet

    // SaaS public DbSet

    #endregion

    public DemoDbContext(DbContextOptions

    }

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

      /* Include modules to your migration db context */
    
      builder.ConfigurePermissionManagement();
      builder.ConfigureSettingManagement();
      builder.ConfigureBackgroundJobs();
      builder.ConfigureAuditLogging();
      builder.ConfigureIdentityPro();
      builder.ConfigureIdentityServer();
      builder.ConfigureFeatureManagement();
      builder.ConfigureLanguageManagement();
      builder.ConfigurePayment();
      builder.ConfigureSaas();
      builder.ConfigureTextTemplateManagement();
      builder.ConfigureBlobStoring();
    
      /*add appuser config*/
      builder.Entity<AppUser>(b =>
      {
          b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser
    
          b.ConfigureByConvention();
          b.ConfigureAbpUser();
    
          /* Configure mappings for your additional properties
           * Also see the OrganizationUnitSampleEfCoreEntityExtensionMappings class
           */
      });
      /* Configure your own tables/entities inside here */
    
      //builder.Entity<YourEntity>(b =>
      //{
      //    b.ToTable(DemoConsts.DbTablePrefix + "YourEntities", DemoConsts.DbSchema);
      //    b.ConfigureByConvention(); //auto configure for the base class props
      //    //...
      //});
    

    }`

  • step4 In the DemoEfcoreEntityExtensionMappings.cs I set the ObjectExtensionManager like below:

  • ` public static void Configure() { DemoGlobalFeatureConfigurator.Configure(); DemoModuleExtensionConfigurator.Configure();

      OneTimeRunner.Run(() =>
      {
          /* You can configure extra properties for the
           * entities defined in the modules used by your application.
           *
           * This class can be used to map these extra properties to table fields in the database.
           *
           * USE THIS CLASS ONLY TO CONFIGURE EF CORE RELATED MAPPING.
           * USE DemoModuleExtensionConfigurator CLASS (in the Domain.Shared project)
           * FOR A HIGH LEVEL API TO DEFINE EXTRA PROPERTIES TO ENTITIES OF THE USED MODULES
           *
           * Example: Map a property to a table field:
    
               ObjectExtensionManager.Instance
                   .MapEfCoreProperty<IdentityUser, string>(
                       "MyProperty",
                       (entityBuilder, propertyBuilder) =>
                       {
                           propertyBuilder.HasMaxLength(128);
                       }
                   );
    
           * See the documentation for more:
           * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities
           */
    
          ObjectExtensionManager.Instance
                    .MapEfCoreProperty<IdentityUser, string>(
                        "IdCard",
                        (entityBuilder, propertyBuilder) =>
                        {
                            propertyBuilder.HasMaxLength(19);
                        }
                    );
      });
    

    }`

  • step5 When I run the *,DbMigrator project,it says "Cannot use table 'AbpUsers' for entity type 'IdentityUser' since it is being used for entity type 'AppUser' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'IdentityUser' on the primary key properties and pointing to the primary key on another entity type mapped to 'AbpUsers'." like below:


5 Answer(s)
  • User Avatar
    0
    abllyboy created

    Any progress?

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, I'll try to reproduce the problem and write you back asap.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    You can use the Table Splitting approach. Please see the Table Splitting documentation of EF Core and then just update your entity configuration as below:

    builder.Entity<AppUser>(b =>
            {
                b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser
    
                b.ConfigureByConvention();
                b.ConfigureAbpUser();
    
                b.HasOne<IdentityUser>().WithOne().HasForeignKey<AppUser>(x => x.Id); //add this line
            });
    
    

    Same with https://github.com/abpframework/abp/issues/8950#issuecomment-1001880208

  • User Avatar
    0
    abllyboy created

    It will generate wrong Column,see the migration below:

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    It will generate wrong Column,see the migration below:

    Actually, this kind of migration generation is normal if you go with the Table Splitting approach, because in this approach basically, you create a subset of an entity by mapping it with another entity.

    As a second option, Can you please check the "The AppUser Entity & Custom Properties " section of this article.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15