在ServiceStack.OrmLite中更改表的别名运行时

时间:2017-08-01 20:29:42

标签: c# servicestack ormlite-servicestack

我有一个包含IdentityDbContext的应用程序,我想使用ServiceStack.OrmLite选择数据,我想知道如何更改别名

public class ApplicationDbContext : IdentityDbContext<User>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<User>().ToTable("User");
        builder.Entity<IdentityRole>().ToTable("Role");
        builder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
        builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim");
        builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin");
    }
}

我在下面尝试了代码,但它无效:

ModelDefinition<User>.Definition.Alias = "Users";
ModelDefinition<IdentityRole>.Definition.Alias = "Roles";
ModelDefinition<IdentityUserRole<string>>.Definition.Alias = "UserRoles";
ModelDefinition<IdentityUserClaim<string>>.Definition.Alias = "UserClaims";
ModelDefinition<IdentityUserLogin<string>>.Definition.Alias = "UserLogins";

1 个答案:

答案 0 :(得分:1)

您可以使用GetModelMetadata()扩展方法更改OrmLite为每个类型使用的表元数据,例如:

var tableMeta = type.GetModelMetadata();
tableMeta.Alias = "NewTableName";