【问题标题】:Trying to change table names in ASP.NET Identity 2.0尝试更改 ASP.NET Identity 2.0 中的表名
【发布时间】:2014-09-05 02:33:35
【问题描述】:

我想更改 ASP.NET Identity 2.0 使用的表的名称。我见过各种类似的问题,但没有什么能解决我的问题。例如这种类型的解决方案: http://www.goatly.net/customizing-table-names-for-identitydbcontextwithcustomuser-data-contexts 似乎取决于代码优先(?)。无论如何,实现 OnModelCreating 对我没有任何帮助。我基本上已经将 AspNetUsers 和类似的表重命名为我自己的名字,并且希望能够使用它们。我有一个想要使用的现有 EF 上下文 (MyContext),所以我修改它以从 IdentityDbContext 派生(编辑 t4 模板),然后在 Startup.Auth 中我有:

        UserManagerFactory = () =>
        {
            var context = new MyContext();

            Database.SetInitializer<MyContext>(new IdentityDbInitializer());

            var userStore = new UserStore<IdentityUser>(context){
                DisposeContext = true
            };

            return new UserManager<IdentityUser>(userStore);
        };

但是当我尝试登录时出现“无法联系服务器”的问题。我想这是因为我的 UserStore 无法知道我的上下文中哪些是 User 表。我希望这是 OnModelCreating 代码会做的事情,但我对此很模糊。我想 UserStore 仍在寻找“AspNetUsers”表,尽管我不知道这些名称来自哪里。我也对为我的上下文修改 t4 模板感到不安 - 这是我应该从 IdentityDbContext 派生的方式吗?

非常感谢任何帮助。

【问题讨论】:

标签: c# entity-framework asp.net-identity-2


【解决方案1】:

几个步骤:

  • 安装 NuGet 包:Microsoft.AspNet.Identity.EntityFramework
  • 在您的 web.config/app.config 中添加 connection string

现在你必须定义你的自定义Database Context

public class MyContext : IdentityDbContext
{
    public MyContext()
        : base(<connection string name>)
    {

    }

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

        modelBuilder.Entity<IdentityUser>()
            .ToTable("Users");

        modelBuilder.Entity<IdentityRole>()
            .ToTable("Roles");

        modelBuilder.Entity<IdentityUserRole>()
            .ToTable("UserRoles");

        modelBuilder.Entity<IdentityUserClaim>()
            .ToTable("UserClaims");

        modelBuilder.Entity<IdentityUserLogin>()
            .ToTable("UserLogins");
    }
}

如您所见,我使用DbModelBuilder 将所有实体映射到新表。

  • 打开 NuGet 包管理器控制台
  • 执行命令Enable-Migrations

它将使用配置文件Configuration.cs创建一个文件夹Migrations

它应该看起来像这样:

internal sealed class Configuration : DbMigrationsConfiguration<ConsoleApplication1.Models.MyContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(ConsoleApplication1.Models.MyContext context)
    {

    }
}

在构造函数中将属性AutomaticMigrationsEnabled 更改为true

  • 打开 NuGet 包管理器控制台
  • 执行命令Update-Database

它应该运行脚本来创建新表。

您可以自定义您的实体(和 Id),为定义的每个接口创建自定义类。

public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim>
{
}

由于您使用的是 Owin,因此您可以定义您的 UserStore:

public class MyUserStore: UserStore<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim>
{
    public MyUserStore(MyContext context)
        : base(context)
    {
    }
}

以及您的 UserManager 实现:

public class ApplicationUserManager : UserManager<ASPNETIdentity2.Models.MyUser, string>
{
    public ApplicationUserManager(IUserStore<ASPNETIdentity2.Models.MyUser, string> store)
        : base(store)
    {

    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var manager = new ApplicationUserManager(new MyUserStore(context.Get<MyContext>()));

        manager.UserValidator = new UserValidator<MyUser, string>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        manager.PasswordValidator = new PasswordValidator()
        {
            RequiredLength = 5,
            RequireNonLetterOrDigit = false,     // true
            // RequireDigit = true,
            RequireLowercase = false,
            RequireUppercase = false,
        };

        return (manager);
    }
}

你的Owin.Startup 应该是这样的:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.CreatePerOwinContext(MyContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    }
}

如果您想查看自定义实现,您可以查看我的 GitHub repository 并使用 ASP.NET MVC 上的简单工作解决方案。

更新:

该解决方案中还有另一个项目,设置最少;基本上,如果您只想重命名表,您只需要定义您的上下文 (IdentityDbContext)。

项目可以在here找到。

【讨论】:

  • 非常感谢您提供此信息。我的问题是,一旦我完成了这个,我可以摆脱代码优先的东西吗,因为我们在这个项目中没有使用 CF?
  • @seesharper:我已经更新了我的答案。检查我的 github 仓库中的链接。
  • 非常感谢@LeftyX - 然而,Identity 2 EF 似乎假定了我们不使用的 Code First。因此我不得不采用另一种解决方案。
  • @LeftyX 一切都按预期工作,但每当我执行更新数据库时,我都会使用一列 (Id) 获取旧的 AspNetUsers
  • @YehiaA.Salam:尝试删除所有表 EXEC sp_MSforeachtable @command1 = "DROP TABLE ?" 并再次运行 Update-Database
【解决方案2】:

带有实体框架的标识 2 假定代码优先。这个项目似乎做我需要的: https://github.com/cbfrank/AspNet.Identity.EntityFramework

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 2013-10-27
    • 2014-05-15
    • 2014-06-30
    • 2016-04-04
    • 2014-05-31
    • 1970-01-01
    • 2014-05-16
    相关资源
    最近更新 更多