Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

I want my table names to be singular. #4254


User avatar
0
ivanberin created

What is the recommended way to make my table names singular, and to remove the abp prefix?


19 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    To remove the Abp prefix, do this:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
       base.OnModelCreating(modelBuilder);
       
       modelBuilder.ChangeAbpTablePrefix<Tenant, Role, User>(""); //Removes table prefixes. You can specify another prefix.
    }
    

    Remember to add using Abp.Zero.EntityFrameworkCore; to your code file in order to access ChangeAbpTablePrefix extension method.

    To make table names singular, create an extension method similar to the implementation of ChangeAbpTablePrefix.

  • User Avatar
    0
    ivanberin created

    Great! Thanks!

    Here's what I'm using in case anyone else is interested.

    public static class ModelBuilderExtensions
        {
            public static void ChangeAbpTablePrefixAndPluralization<TTenant, TRole, TUser>(this ModelBuilder modelBuilder, string prefix, string schemaName = null, bool plural = true)
                where TTenant : AbpTenant<TUser>
                where TRole : AbpRole<TUser>
                where TUser : AbpUser<TUser>
            {
                prefix = prefix ?? "";
    
                var s = "";
    
                if (plural)
                {
                    s = "s";
                }
    
                SetTableName<AuditLog>(modelBuilder, prefix + "AuditLog" + s, schemaName);
                SetTableName<BackgroundJobInfo>(modelBuilder, prefix + "BackgroundJob" + s, schemaName);
                SetTableName<Edition>(modelBuilder, prefix + "Edition" + s, schemaName);
                SetTableName<FeatureSetting>(modelBuilder, prefix + "Feature" + s, schemaName);
                SetTableName<TenantFeatureSetting>(modelBuilder, prefix + "Feature" + s, schemaName);
                SetTableName<EditionFeatureSetting>(modelBuilder, prefix + "Feature" + s, schemaName);
                SetTableName<ApplicationLanguage>(modelBuilder, prefix + "Language" + s, schemaName);
                SetTableName<ApplicationLanguageText>(modelBuilder, prefix + "LanguageText" + s, schemaName);
                SetTableName<NotificationInfo>(modelBuilder, prefix + "Notification" + s, schemaName);
                SetTableName<NotificationSubscriptionInfo>(modelBuilder, prefix + "NotificationSubscription" + s, schemaName);
                SetTableName<OrganizationUnit>(modelBuilder, prefix + "OrganizationUnit" + s, schemaName);
                SetTableName<PermissionSetting>(modelBuilder, prefix + "Permission" + s, schemaName);
                SetTableName<RolePermissionSetting>(modelBuilder, prefix + "Permission" + s, schemaName);
                SetTableName<UserPermissionSetting>(modelBuilder, prefix + "Permission" + s, schemaName);
                SetTableName<TRole>(modelBuilder, prefix + "Role" + s, schemaName);
                SetTableName<Setting>(modelBuilder, prefix + "Setting" + s, schemaName);
                SetTableName<TTenant>(modelBuilder, prefix + "Tenant" + s, schemaName);
                SetTableName<UserLogin>(modelBuilder, prefix + "UserLogin" + s, schemaName);
                SetTableName<UserLoginAttempt>(modelBuilder, prefix + "UserLoginAttempt" + s, schemaName);
                SetTableName<TenantNotificationInfo>(modelBuilder, prefix + "TenantNotification" + s, schemaName);
                SetTableName<UserNotificationInfo>(modelBuilder, prefix + "UserNotification" + s, schemaName);
                SetTableName<UserOrganizationUnit>(modelBuilder, prefix + "UserOrganizationUnit" + s, schemaName);
                SetTableName<UserRole>(modelBuilder, prefix + "UserRole" + s, schemaName);
                SetTableName<TUser>(modelBuilder, prefix + "User" + s, schemaName);
                SetTableName<UserAccount>(modelBuilder, prefix + "UserAccount" + s, schemaName);
                SetTableName<UserClaim>(modelBuilder, prefix + "UserClaim" + s, schemaName);
                SetTableName<RoleClaim>(modelBuilder, prefix + "RoleClaim" + s, schemaName);
                SetTableName<UserToken>(modelBuilder, prefix + "UserToken" + s, schemaName);
            }
    
            internal static void SetTableName<TEntity>(this ModelBuilder modelBuilder, string tableName, string schemaName)
                where TEntity : class
            {
                if (schemaName == null)
                {
                    modelBuilder.Entity<TEntity>().ToTable(tableName);
                }
                else
                {
                    modelBuilder.Entity<TEntity>().ToTable(tableName, schemaName);
                }
            }
        }
    
  • User Avatar
    0
    bbakermmc created

    Or just add an attribute to the class for "table" that strips off the s.

  • User Avatar
    0
    ivanberin created

    <cite>BBakerMMC: </cite> Or just add an attribute to the class for "table" that strips off the s.

    Yes, I did it in combination with the extension method.

  • User Avatar
    0
    bolenton created

    When ever I run this command I get this error. => System.Data.SqlClient.SqlException: Invalid object name 'Editions'.

  • User Avatar
    0
    alper created
    Support Team

    By the way there's an entity framework way of doing this

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
           ...
        }
    
  • User Avatar
    0
    bolenton created

    My issue is I get that error when I try to remove the Abp Prefix.

  • User Avatar
    0
    bolenton created

    When I run the command to remove the Table Name prefix, I get this error. Does anyone know what I am doing wrong? System.Data.SqlClient.SqlException: Invalid object name 'Editions'.

  • User Avatar
    0
    aaron created
    Support Team

    Is that during Update-Database or application startup? Can you check if the table name is changed in your database?

  • User Avatar
    0
    bolenton created

    it was during update database, So I wiped the database and started fresh by running the migrator console app. I get the same error.

  • User Avatar
    0
    aaron created
    Support Team

    Like I asked, can you check if the table name is changed in your database?

  • User Avatar
    0
    bolenton created

    table names are unchanged

  • User Avatar
    0
    aaron created
    Support Team

    Can you show your OnModelCreating method?

  • User Avatar
    0
    bolenton created
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.ChangeAbpTablePrefixAndPluralization<Tenant, Role, User>("");
    
        modelBuilder.Entity<MemberStatus>(b =>
        {
            b.HasIndex(e => new { e.TenantId });
        });
    
        modelBuilder.Entity<BinaryObject>(b =>
        {
            b.HasIndex(e => new { e.TenantId });
        });
    
        modelBuilder.Entity<ChatMessage>(b =>
        {
            b.HasIndex(e => new { e.TenantId, e.UserId, e.ReadState });
            b.HasIndex(e => new { e.TenantId, e.TargetUserId, e.ReadState });
            b.HasIndex(e => new { e.TargetTenantId, e.TargetUserId, e.ReadState });
            b.HasIndex(e => new { e.TargetTenantId, e.UserId, e.ReadState });
        });
    
        modelBuilder.Entity<Friendship>(b =>
        {
            b.HasIndex(e => new { e.TenantId, e.UserId });
            b.HasIndex(e => new { e.TenantId, e.FriendUserId });
            b.HasIndex(e => new { e.FriendTenantId, e.UserId });
            b.HasIndex(e => new { e.FriendTenantId, e.FriendUserId });
        });
    
        modelBuilder.Entity<Tenant>(b =>
        {
            b.HasIndex(e => new { e.SubscriptionEndDateUtc });
            b.HasIndex(e => new { e.CreationTime });
        });
    
        modelBuilder.Entity<SubscriptionPayment>(b =>
        {
            b.HasIndex(e => new { e.Status, e.CreationTime });
            b.HasIndex(e => new { e.PaymentId, e.Gateway });
        });
    
        modelBuilder.ConfigurePersistedGrantEntity();
    }
    
    
    public static void ChangeAbpTablePrefixAndPluralization<TTenant, TRole, TUser>(this ModelBuilder modelBuilder, string prefix, string schemaName = null, bool plural = true)
                where TTenant : AbpTenant<TUser>
                where TRole : AbpRole<TUser>
                where TUser : AbpUser<TUser>
            {
                prefix = prefix ?? "";
    
                var s = "";
    
                if (plural)
                {
                    s = "s";
                }
    
                SetTableName<AuditLog>(modelBuilder, prefix + "AuditLogs", schemaName);
                SetTableName<BackgroundJobInfo>(modelBuilder, prefix + "BackgroundJobs", schemaName);
                SetTableName<Edition>(modelBuilder, prefix + "AbpEditions", schemaName);
                SetTableName<ApplicationLanguage>(modelBuilder, prefix + "Languages", schemaName);
                SetTableName<ApplicationLanguageText>(modelBuilder, prefix + "LanguageTexts", schemaName);
                SetTableName<NotificationInfo>(modelBuilder, prefix + "Notifications", schemaName);
                SetTableName<NotificationSubscriptionInfo>(modelBuilder, prefix + "NotificationSubscriptions", schemaName);
                SetTableName<OrganizationUnit>(modelBuilder, prefix + "Groups", schemaName);
                SetTableName<PermissionSetting>(modelBuilder, prefix + "Permissions", schemaName);
                SetTableName<RolePermissionSetting>(modelBuilder, prefix + "RolePermissions", schemaName);
                SetTableName<UserPermissionSetting>(modelBuilder, prefix + "MemberPermissions", schemaName);
                SetTableName<TRole>(modelBuilder, prefix + "Roles", schemaName);
                SetTableName<Setting>(modelBuilder, prefix + "Settings", schemaName);
                SetTableName<UserLogin>(modelBuilder, prefix + "MemberLogins", schemaName);
                SetTableName<UserLoginAttempt>(modelBuilder, prefix + "MemberLoginAttempts", schemaName);
                SetTableName<TenantNotificationInfo>(modelBuilder, prefix + "TenantNotifications", schemaName);
                SetTableName<UserNotificationInfo>(modelBuilder, prefix + "MemberNotifications", schemaName);
                SetTableName<UserOrganizationUnit>(modelBuilder, prefix + "MemberGroups", schemaName);
                SetTableName<UserRole>(modelBuilder, prefix + "MemberRoles", schemaName);
                SetTableName<TUser>(modelBuilder, prefix + "Members", schemaName);
                SetTableName<UserAccount>(modelBuilder, prefix + "MemberAccounts", schemaName);
                SetTableName<UserClaim>(modelBuilder, prefix + "MemberClaims", schemaName);
                SetTableName<RoleClaim>(modelBuilder, prefix + "RoleClaims", schemaName);
                SetTableName<UserToken>(modelBuilder, prefix + "MemberTokens", schemaName);
            }
    
            internal static void SetTableName<TEntity>(this ModelBuilder modelBuilder, string tableName, string schemaName) where TEntity : class
            {
                if (schemaName == null)
                {
                    modelBuilder.Entity<TEntity>().ToTable(tableName);
                }
                else
                {
                    modelBuilder.Entity<TEntity>().ToTable(tableName, schemaName);
                }
            }
    
  • User Avatar
    0
    aaron created
    Support Team

    Can you try using Abp.Zero.EntityFrameworkCore; and ChangeAbpTablePrefix?

  • User Avatar
    0
    bolenton created

    I did that first. I get the exact same error.

  • User Avatar
    0
    aaron created
    Support Team
    • Does it work if you comment out ChangeAbpTablePrefix?
    • Are you using the default migrations provided by Asp.Net Zero?
    • Can you show what's in the __MigrationHistory table in your database?
  • User Avatar
    0
    bolenton created

    Does it work if you comment out ChangeAbpTablePrefix?

    • Yes Are you using the default migrations provided by Asp.Net Zero?
    • Yes Can you show what's in the __MigrationHistory table in your database?

    MigrationId || ProductVersion 20170406083347_Initial_Migration || 2.0.1-rtm-125 20170623075109_AspNetZero_V4_1_Changes || 2.0.1-rtm-125 20170704084731_Added_GoogleAuthenticatorKey_Column || 2.0.1-rtm-125 20170714081027_Added_Relation_Between_Edition_And_SubscriptionPayment || 2.0.1-rtm-125 20170724142223_Upgraded_To_Abp_V2_2 || 2.0.1-rtm-125 20170913133916_Added_SharedMessageId_To_ChatMessage || 2.0.1-rtm-125 20170914070123_Added_ReceiverReadState_To_ChatMessage || 2.0.1-rtm-125 20170914084815_Invoice_Changes || 2.0.1-rtm-125 20170914121022_TypeChange_SharedMessageId_String_Guid || 2.0.1-rtm-125 20171128060602_Added_MoreFeildsTo_User_Entity || 2.0.1-rtm-125 20171128061636_Added_More_UserFeilds_Entity || 2.0.1-rtm-125

  • User Avatar
    0
    bolenton created

    Issue Resolved

    Thanks