abp icon indicating copy to clipboard operation
abp copied to clipboard

Adding a new field to the Abp.io base tables like AbpRoles

Open nishadebrahim opened this issue 6 years ago • 2 comments

As per the Abp.io document https://docs.abp.io/en/abp/latest/Entity-Framework-Core-Migrations , I tried to add a new field 'Title' in AppRole class

[Audited] [System.ComponentModel.DataAnnotations.Schema.Table("AbpRoles")]

public class AppRole : AggregateRoot, IHasConcurrencyStamp, IHasExtraProperties {

......

public virtual string Title { get; set; }

.............

}

In ApiDbContext, i have added the below lines,

builder.Entity<AppRole>(b =>

        {

            b.ToTable("AbpRoles");

            b.ConfigureByConvention();

            b.Property(x => x.Title).HasMaxLength(128);


        });
Then same has been added to ApiMigrationsDbContext class

builder.Entity(b =>

        {

            b.ToTable("AbpRoles");

            b.ConfigureByConvention();

            b.Property(x => x.Title).HasMaxLength(128);


        });

I am getting the following error during migration

Cannot use table 'AbpRoles' for entity type 'IdentityRole' since it is being used for entity type 'AppRole' and there is no relationship between their primary keys.

The document talks about the following approach also. But i am not able to find the ObjectExtensionManager.


private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();

public static void Configure()

{
    OneTimeRunner.Run(() =>
    {
        ObjectExtensionManager.Instance
            .MapEfCoreProperty<IdentityRole, string>(
                "Title",
                builder => { builder.HasMaxLength(64); }
            );
    });

}
}

What I am missing here. Your help will be highly appreciated. Thanks

nishadebrahim avatar Jun 08 '20 16:06 nishadebrahim

Hi @nishadebrahim

This documentation can help you; Customizing the Application Modules: Extending Entities,

And also it will help while extending user interface; ASP.NET Core (MVC / Razor Pages) User Interface Customization Guide

cotur avatar Jun 08 '20 19:06 cotur

You shouldn't add it to ApiMigrationsDbContext at all. There is a point in the startup template that you can safely do it: YourProjectNameEfCoreEntityExtensionMappings

see the document: https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities

Reopen if you have an issue.

hikalkan avatar Jun 14 '20 15:06 hikalkan