C# 正在尝试更改ASP.NET Identity 2.0中的表名

C# 正在尝试更改ASP.NET Identity 2.0中的表名,c#,entity-framework,asp.net-identity-2,C#,Entity Framework,Asp.net Identity 2,我想更改ASP.NET Identity 2.0使用的表的名称。我见过很多类似的问题,但都不能解决我的问题。例如,这种类型的解决方案: 似乎首先依赖于代码(?)。无论如何,实现OnModelCreating对我来说毫无用处。我基本上已经将AspNetUsers和类似的表重命名为我自己的名称,并希望能够使用这些名称。我有一个要使用的现有EF上下文(MyContext),因此我将其修改为从IdentityDbContext(编辑t4模板)派生,然后在启动时进行验证。我有: UserM

我想更改ASP.NET Identity 2.0使用的表的名称。我见过很多类似的问题,但都不能解决我的问题。例如,这种类型的解决方案: 似乎首先依赖于代码(?)。无论如何,实现OnModelCreating对我来说毫无用处。我基本上已经将AspNetUsers和类似的表重命名为我自己的名称,并希望能够使用这些名称。我有一个要使用的现有EF上下文(MyContext),因此我将其修改为从IdentityDbContext(编辑t4模板)派生,然后在启动时进行验证。我有:

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

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

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

            return new UserManager<IdentityUser>(userStore);
        };
UserManagerFactory=()=>
{
var context=new MyContext();
SetInitializer(新的identityBinInitializer());
var userStore=新的userStore(上下文){
DisposeContext=true
};
返回新的UserManager(userStore);
};
但当我尝试登录时,出现了“无法联系服务器”的问题。我想这是因为我的UserStore无法知道我的上下文中哪些是用户表。我希望OnModelCreating代码能做到这一点,但对此我并不清楚。我想UserStore仍然在寻找一个“AspNetUsers”表,尽管我不知道这些名称来自哪里。我也对修改t4模板以适应我的上下文感到不安——这是我应该从IdentityDbContext派生的方式吗

非常感谢您的帮助。

请遵循以下几个步骤:

  • 安装NuGet软件包:
    Microsoft.AspNet.Identity.EntityFramework
  • 连接字符串添加到web.config/app.config
现在,您必须定义自定义的
数据库上下文

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");
    }
}
在构造函数中,将属性
AutomaticMigrationsEnabled
更改为
true

  • 打开NuGet包管理器控制台
  • 执行命令更新数据库
它应该运行脚本来创建新表

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

public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim>
{
}
如果您想了解自定义实现,可以使用ASP.NET MVC上的简单工作解决方案检查my

更新:

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


可以找到该项目。

Identity 2 with Entity Framework首先假定代码。该项目似乎满足了我的需要:

非常感谢您提供此信息。我的问题是,一旦我完成了这项工作,我能否摆脱代码优先的东西,因为我们在这个项目中没有使用CF?@seesharper:我已经更新了我的答案。查看我的github repo中的链接。非常感谢@LeftyX-不过,似乎Identity 2 EF首先假定代码,而我们不使用代码。因此,我不得不采用另一种解决方案。@LeftyX一切正常,但每当我进行更新时,我总是让旧的AspNetUsers使用一列(Id)-Database@YehiaA.Salam:尝试删除所有表格
EXEC sp_MSforeachtable@command1=“DROP TABLE?”
并再次运行
更新数据库
。如果您对我的答案感到满意,请接受好吗?干杯。@请看关于我问题的任何建议
public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim>
{
}
public class MyUserStore: UserStore<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim>
{
    public MyUserStore(MyContext context)
        : base(context)
    {
    }
}
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);
    }
}
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.CreatePerOwinContext(MyContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    }
}