Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Created March 2, 2016 12:54
Show Gist options
  • Save hikalkan/4f8f793c7929a81d300f to your computer and use it in GitHub Desktop.
Save hikalkan/4f8f793c7929a81d300f to your computer and use it in GitHub Desktop.
Change prefix ABP tables.
using System.Data.Entity;
using Abp.Application.Editions;
using Abp.Application.Features;
using Abp.Auditing;
using Abp.Authorization;
using Abp.Authorization.Roles;
using Abp.Authorization.Users;
using Abp.BackgroundJobs;
using Abp.Configuration;
using Abp.Localization;
using Abp.MultiTenancy;
using Abp.Notifications;
using Abp.Organizations;
namespace Abp.Zero.EntityFramework
{
/// <summary>
/// Extension methods for <see cref="DbModelBuilder"/>.
/// </summary>
public static class AbpZeroDbModelBuilderExtensions
{
/// <summary>
/// Changes prefix for ABP tables (which is "Abp" by default).
/// Can be null/empty string to clear the prefix.
/// </summary>
/// <typeparam name="TTenant">The type of the tenant entity.</typeparam>
/// <typeparam name="TRole">The type of the role entity.</typeparam>
/// <typeparam name="TUser">The type of the user entity.</typeparam>
/// <param name="modelBuilder">Model builder.</param>
/// <param name="prefix">Table prefix, or null to clear prefix.</param>
public static void ChangeAbpTablePrefix<TTenant,TRole,TUser>(this DbModelBuilder modelBuilder, string prefix)
where TTenant : AbpTenant<TTenant, TUser>
where TRole : AbpRole<TTenant, TUser>
where TUser : AbpUser<TTenant, TUser>
{
prefix = prefix ?? "";
modelBuilder.Entity<AuditLog>().ToTable(prefix + "AuditLogs");
modelBuilder.Entity<BackgroundJobInfo>().ToTable(prefix + "BackgroundJobs");
modelBuilder.Entity<Edition>().ToTable(prefix + "Editions");
modelBuilder.Entity<FeatureSetting>().ToTable(prefix + "Features");
modelBuilder.Entity<TenantFeatureSetting>().ToTable(prefix + "Features");
modelBuilder.Entity<EditionFeatureSetting>().ToTable(prefix + "Features");
modelBuilder.Entity<ApplicationLanguage>().ToTable(prefix + "Languages");
modelBuilder.Entity<ApplicationLanguageText>().ToTable(prefix + "LanguageTexts");
modelBuilder.Entity<NotificationInfo>().ToTable(prefix + "Notifications");
modelBuilder.Entity<NotificationSubscriptionInfo>().ToTable(prefix + "NotificationSubscriptions");
modelBuilder.Entity<OrganizationUnit>().ToTable(prefix + "OrganizationUnits");
modelBuilder.Entity<PermissionSetting>().ToTable(prefix + "Permissions");
modelBuilder.Entity<RolePermissionSetting>().ToTable(prefix + "Permissions");
modelBuilder.Entity<UserPermissionSetting>().ToTable(prefix + "Permissions");
modelBuilder.Entity<TRole>().ToTable(prefix + "Roles");
modelBuilder.Entity<Setting>().ToTable(prefix + "Settings");
modelBuilder.Entity<TTenant>().ToTable(prefix + "Tenant");
modelBuilder.Entity<UserLogin>().ToTable(prefix + "UserLogins");
modelBuilder.Entity<UserNotificationInfo>().ToTable(prefix + "UserNotifications");
modelBuilder.Entity<UserOrganizationUnit>().ToTable(prefix + "UserOrganizationUnits");
modelBuilder.Entity<UserRole>().ToTable(prefix + "UserRoles");
modelBuilder.Entity<TUser>().ToTable(prefix + "Users");
}
}
}
@hikalkan
Copy link
Author

hikalkan commented Mar 2, 2016

Example 1: Remove table prefix:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.ChangeAbpTablePrefix<Tenant, Role, User>("");
}

Example 2: Change to another prefix:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.ChangeAbpTablePrefix<Tenant, Role, User>("MyPrefix");
}

AbpZeroDbModelBuilderExtensions class will be included to Abp.Zero nuget package in the next version (https://github.com/aspnetboilerplate/module-zero/issues/165).
Currently, you can copy in into your project.

@automys
Copy link

automys commented Apr 6, 2016

For future reference, to implement this:

  1. Add the OnModelCreating override block to your DbContext class in EntityFramework project.
  2. In package manager console, add a migration e.g. "Add-Migration 'Remove_Abp_Prefixes'"
  3. Update the database: "Update-Database"

@worthy7
Copy link

worthy7 commented Jan 20, 2017

This overrides any custom attributes placed on Entities [Table("AppUser")]

@ebegen
Copy link

ebegen commented Oct 17, 2017

I add onModelCreating to my dbContext like below;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.ChangeAbpTablePrefix<Tenant, Role, User>("MyPrefix");
}

(Of course i changed "MyPrefix")
After that i typed "update-database" in package manager console. After i looked database tables i saw they are empty. So seed method is not working.

How can i solve this problem?

@bolenton
Copy link

When I use this, I keep on getting this errors: "Invalid object name 'Editions." Does anyone know how to remove thew Abp prefix from all table without error. This is a brand new project. I haven't added any custom code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment