Created
March 2, 2016 12:54
-
-
Save hikalkan/4f8f793c7929a81d300f to your computer and use it in GitHub Desktop.
Change prefix ABP tables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
For future reference, to implement this:
- Add the OnModelCreating override block to your DbContext class in EntityFramework project.
- In package manager console, add a migration e.g. "Add-Migration 'Remove_Abp_Prefixes'"
- Update the database: "Update-Database"
This overrides any custom attributes placed on Entities [Table("AppUser")]
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?
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
Example 1: Remove table prefix:
Example 2: Change to another prefix:
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.