Base solution for your next web application
Open Closed

Is it possible to remove "Abp" prefix from all table names #684


User avatar
0
niners481 created

Hello Sir,

Is it possible to remove "Abp" prefix from the table name? For example: I want my table to be call AuditLogs instead of AbpEditLogs.

I thought this could be accomplish by updating the migration scripts before running "Update-Database" in the Package Manager Console. I did a search and replace all instances of "dbo.AbpEditLogs" to "dbo.EditLogs" on all the scripts.

The "Update-Database" was able to apply all the migration, however it failed when it attempt run the Seed method.

Error message: System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid object name 'dbo.AbpEditions'. ... ... at MyTestAbpProject.Migrations.SeedData.DefaultEditionsBuilder.CreateEditions() in C:\MyTestAbpProject\MyTestAbpProject.EntityFramework\Migrations\SeedData\DefaultEditionsBuilder.cs:line 24 at MyTestAbpProject.Migrations.SeedData.DefaultEditionsBuilder.Build() in C:MyTestAbpProject\MyTestAbpProject.EntityFramework\Migrations\SeedData\DefaultEditionsBuilder.cs:line 19 at MyTestAbpProject.Migrations.SeedData.InitialDataBuilder.Build() in C:\MyTestAbpProject\MyTestAbpProject.EntityFramework\Migrations\SeedData\InitialDataBuilder.cs:line 19 at MyTestAbpProject.Migrations.Configuration.Seed(MyTestAbpProjectDbContext context) in C:\MyTestAbpProject\MyTestAbpProject.EntityFramework\Migrations\Configuration.cs:line 16 at System.Data.Entity.Migrations.DbMigrationsConfiguration1.OnSeed(DbContext context) at System.Data.Entity.Migrations.DbMigrator.SeedDatabase() at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase() at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1 pendingMigrations, String targetMigrationId, String lastMigrationId) at ... ...

Looking at DefaultEditionsBuilder.cs line# 24, var defaultEdition = _context.Editions.FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);

Did find implementation on _context.Editions which eventually lead me to

namespace Abp.Application.Editions { [Table("AbpEditions")] public class Edition : FullAuditedEntity

It appears that the [Table("AbpEditions")] is in your Apb library and I have no way of removing the "Abp" table prefix.

Is there a workaround?

Thanks in advance for your help. -Peter


6 Answer(s)
  • User Avatar
    0
    omar created

    You should be able to change the name of your table during OnModelCreating. You can overwrite DataAnnotation of the table name . Code-First gives precedence to Fluent API > data annotations > default conventions.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
      
                modelBuilder.Entity<AuditLog>().ToTable("AuditLogs ");
                        
            }
    
  • User Avatar
    0
    vincedc created

    <cite>Omar: </cite> You should be able to change the name of your table during OnModelCreating. You can overwrite DataAnnotation of the table name . Code-First gives precedence to Fluent API > data annotations > default conventions.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
           {
     
               modelBuilder.Entity<AuditLog>().ToTable("AuditLogs ");
                       
           }
    

    How to handle the derived entities which actually are mapped with Table-per-type strategy? For example, in the default project

    RolePermissionSetting
    

    and

    UserPermissionSetting
    

    are both mapped to

    AbpPermissions
    

    table. How can we mapped these inheritance relationships?

  • User Avatar
    0
    nmtoan07 created

    I have the same problem too... Anyone helps...

  • User Avatar
    0
    hikalkan created
    Support Team

    Have you tried overriding modelBuilder as described above. You can make it for also derived entities.

  • User Avatar
    0
    nmtoan07 created

    Thanks for your reply. Could we define a solution for this, because i really want to use your current entities. I thought a solution: FlexibleTableNameAttribute : TableNameAttribute And in that attribute, we use a resource file for TableName, all table name will be handle in 1 file. When someone want to change TableName, it will be easier :D Currently, we don't know how many TableName existed in AbpFramework. With me, i must run update-database and see how many table in database to figure out.

    Thanks and best regards

  • User Avatar
    0
    hikalkan created
    Support Team

    OK, get the code: <a class="postlink" href="https://gist.github.com/hikalkan/4f8f793c7929a81d300f">https://gist.github.com/hikalkan/4f8f793c7929a81d300f</a>