尝试在ASP.NET Identity 2.0中更改表名

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

UserManagerFactory = () => { var context = new MyContext(); Database.SetInitializer(new IdentityDbInitializer()); var userStore = new UserStore(context){ DisposeContext = true }; return new UserManager(userStore); }; 

但是当我尝试登录时出现问题,我得到“无法联系服务器”。 我想这是因为我的UserStore无法知道我的上下文中的User表。 我希望这是OnModelCreating代码可以做的,但我对此很朦胧。 我想UserStore仍然在寻找一个“AspNetUsers”表,虽然我不知道这些名字来自哪里。 我也很难为我的上下文修改t4模板 – 这是我应该从IdentityDbContext派生的方式吗?

任何帮助非常感谢。

几个步骤:

  • 安装NuGet包: Microsoft.AspNet.Identity.EntityFramework
  • connection string添加到web.config / app.config

现在您必须定义自定义Database Context

 public class MyContext : IdentityDbContext { public MyContext() : base() { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .ToTable("Users"); modelBuilder.Entity() .ToTable("Roles"); modelBuilder.Entity() .ToTable("UserRoles"); modelBuilder.Entity() .ToTable("UserClaims"); modelBuilder.Entity() .ToTable("UserLogins"); } } 

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

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

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

它应该看起来像这样:

 internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(ConsoleApplication1.Models.MyContext context) { } } 

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

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

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

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

 public class MyUser : IdentityUser { } 

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

 public class MyUserStore: UserStore { public MyUserStore(MyContext context) : base(context) { } } 

和你的UserManager实现:

 public class ApplicationUserManager : UserManager { public ApplicationUserManager(IUserStore store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context) { var manager = new ApplicationUserManager(new MyUserStore(context.Get())); manager.UserValidator = new UserValidator(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.Create); } } 

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

更新:

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

该项目可以在这里找到。

具有entity framework的身份2假定代码优先。 这个项目似乎做我需要的: https : //github.com/cbfrank/AspNet.Identity.EntityFramework