尝试更改 ASP.NET Identity 2.0 中的表名 [英] Trying to change table names in ASP.NET Identity 2.0

查看:36
本文介绍了尝试更改 ASP.NET Identity 2.0 中的表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to change the names of the tables used by ASP.NET Identity 2.0. I've seen various similar questions but nothing that solves my problem. For instance this type of solution: http://www.goatly.net/customizing-table-names-for-identitydbcontextwithcustomuser-data-contexts seems to depend on Code First(?). At any rate implementing OnModelCreating does nothing for me. I've basically renamed the AspNetUsers and similar tables to my own names and want to be able to use these. I have an existing EF context (MyContext) that I want to use, so I modified it to derive from IdentityDbContext (editing the t4 template), and then in Startup.Auth I have:

        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 派生的方式吗?

But the problem occurs when I try to log in that I get "cannot contact server". I imagine that is because my UserStore has no way of knowing which are the User tables on my context. I'd hoped this is what the OnModelCreating code would do, but I'm hazy on this. I imagine UserStore is still looking for an "AspNetUsers" table, though I don't know where these names come from. I'm also uneasy about modifying the t4 template for my context - is this the way I'm supposed to derive from IdentityDbContext?

非常感谢任何帮助.

推荐答案

要遵循的几个步骤:

  • 安装 NuGet 包:Microsoft.AspNet.Identity.EntityFramework
  • 连接字符串添加到您的 web.config/app.config
  • Install NuGet Package: Microsoft.AspNet.Identity.EntityFramework
  • Add a connection string to your web.config/app.config

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

Now you have to define your custom 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 将所有实体映射到新表.

As you can see I've used DbModelBuilder to map all the entities to a new tables.

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

它将创建一个文件夹Migrations,其中包含配置文件Configuration.cs.

It will create a folder Migrations with the configuration file Configuration.cs.

它应该看起来像这样:

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

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

    }
}

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

In the constructor change the property AutomaticMigrationsEnabled to true.

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

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

It should run the script to create the new tables.

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

You can customize your entities (and Ids) creating custom class for each interface defined.

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

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

Since you're using Owin you can define your UserStore:

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

和您的 UserManager 实现:

and your UserManager implementation:

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 应该是这样的:

And your Owin.Startup should look something like this:

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

如果您想查看自定义实现,您可以查看我的 GitHub存储库,在 ASP.NET MVC 上有一个简单的工作解决方案.

If you want to have a look at a custom implementation you can check my GitHub repository with a simple working solution on ASP.NET MVC.

更新:

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

There's another project in that solution with a minimal setup; basically you only need to define your context (IdentityDbContext) if you only want to rename your tables.

可以在此处找到该项目.

这篇关于尝试更改 ASP.NET Identity 2.0 中的表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆