试图改变在ASP.NET 2.0身份表名 [英] Trying to change table names in ASP.NET Identity 2.0

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

问题描述

我要改变由ASP.NET 2.0标识使用的表的名称。我已经看到了解决我的问题的各种类似的问题,但一无所获。例如这种类型的解决方案:
http://www.goatly.net /定制表-名换identitydbcontextwithcustomuser数据 - 上下文
似乎取决于代码优先(?)。在实施OnModelCreating无论如何什么也不做对我来说。我基本上改名为AspNetUsers和类似的表我自己的名字,并希望能够使用这些。我有我要使用现有的EF上下文(MyContext),所以我修改了它从IdentityDbContext(编辑T4模板)得出,然后在Startup.Auth我有:

  UserManagerFactory =()=> 
{
VAR背景=新MyContext();

Database.SetInitializer&所述; MyContext>(新IdentityDbInitializer());

变种userStore =新UserStore< IdentityUser>(上下文){
DisposeContext = TRUE
};

返回新的UserManager< IdentityUser>(userStore);
};



但是,当我尝试登录,我得到无法连接服务器出现问题。我想这是因为我的UserStore无法知道这是对我的上下文中的用户表的方式。我所希望的,这是什么OnModelCreating代码会做,但我对这个朦胧。我想象UserStore还在寻找一个AspNetUsers表中,虽然我不知道这些名字从何而来。我也感到不安有关修改T4模板,我的背景下 - 这是我应该从IdentityDbContext派生方式



任何帮助,不胜感激。

解决方案

几个步骤如下:




  • 安装的NuGet包: Microsoft.AspNet.Identity.EntityFramework

  • 添加连接字符串到你的web.config /的app.config



现在,你必须定义自定义数据库上下文

 公共类MyContext:IdentityDbContext 
{
公共MyContext()
:基地(小于连接字符串名称>)
{

}

保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
base.OnModelCreating(模型构建器);

modelBuilder.Entity< IdentityUser>()
.ToTable(用户);

modelBuilder.Entity< IdentityRole>()
.ToTable(角色);

modelBuilder.Entity< IdentityUserRole>()
.ToTable(的UserRole);

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

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



正如你可以看到我已经使用 DbModelBuilder 来的所有实体到一个新的表映射。




  • 开启的NuGet包管理器控制台

  • 执行命令启用的迁移



这将创建一个文件夹迁移通过。配置文件 Configuration.cs



它应该是这个样子:

 内部密封类配置:DbMigrationsConfiguration< ConsoleApplication1.Models.MyContext> 
{
公共配置()
{
AutomaticMigrationsEnabled = FALSE;
}

保护覆盖无效种子(ConsoleApplication1.Models.MyContext上下文)
{

}
}

在构造函数中的属性 AutomaticMigrationsEnabled 更改为真正




  • 开启的NuGet包管理器控制台

  • 执行命令更新 - 数据库



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



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

 公共类MYUSER:IdentityUser<字符串,MyUserLogin,MyUserRole,MyUserClaim> 
{
}



由于您使用Owin您可以定义UserStore

 公共类MyUserStore:UserStore< MYUSER,MyRole,串,MyUserLogin,MyUserRole,MyUserClaim> 
{
公共MyUserStore(MyContext上下文)
:基地(上下文)
{
}
}

和您的UserManager实现:

 公共类ApplicationUserManager :&的UserManager LT; ASPNETIdentity2.Models.MyUser,串> 
{
公共ApplicationUserManager(IUserStore< ASPNETIdentity2.Models.MyUser,串>存储)
:基(存储)
{

}

酒店的公共静态ApplicationUserManager创建(IdentityFactoryOptions< ApplicationUserManager>选项,IOwinContext上下文)
{
变种经理=新ApplicationUserManager(新MyUserStore(context.Get< MyContext>()));

manager.UserValidator =新UserValidator< MYUSER,串>(经理)
{
AllowOnlyAlphanumericUserNames =假,
RequireUniqueEmail = TRUE
};

manager.PasswordValidator =新PasswordValidator()
{
RequiredLength = 5,
RequireNonLetterOrDigit =假,真//
// RequireDigit = TRUE,
RequireLowercase =假,
RequireUppercase =假,
};

回报(经理);
}
}

和您的 Owin.Startup 应该是这个样子:

 公共类启动
{
公无效配置(IAppBuilder应用程序)
{
app.CreatePerOwinContext(MyContext.Create);
app.CreatePerOwinContext< ApplicationUserManager>(ApplicationUserManager.Create);
}
}

如果你想看看一个自定义实现你可以检查我的的GitHub库与ASP.NET MVC一个简单有效的解决方案。



更新:



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



该项目可以发现的这里


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);
        };

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?

Any help greatly appreciated.

解决方案

Few steps to follow:

  • Install NuGet Package: Microsoft.AspNet.Identity.EntityFramework
  • Add a connection string to your web.config/app.config

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");
    }
}

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

  • Open NuGet Package Manager Console
  • Execute command Enable-Migrations

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

It should look something like this:

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

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

    }
}

In the constructor change the property AutomaticMigrationsEnabled to true.

  • Open NuGet Package Manager Console
  • Execute command Update-Database

It should run the script to create the new tables.

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

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

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)
    {
    }
}

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);
    }
}

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);
    }
}

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.

UPDATE:

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.

The project can be found here.

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

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