ASP.Net Identity如何设置目标DB?

Nef*_*ion 23 c# asp.net asp.net-mvc asp.net-identity

我正在使用Asp-Team 的ASP.NET Identity Sample,我正在尝试更改数据库IdentityDbContext...

我用构造函数试了一下

public MyDbContext() : base("MyConnectionString") { } // base is IdentityDbContext
Run Code Online (Sandbox Code Playgroud)

和DbContext类一样.

这很有效,直到我尝试注册用户...

await IdentityStore.CreateLocalUser(user, model.Password)
Run Code Online (Sandbox Code Playgroud)

返回false......没有错误,没有.

任何想法如何解决?

编辑:

文件名是Models\AppModel.cs,有MyDbContext class

原来是

public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
}
Run Code Online (Sandbox Code Playgroud)

我改成了

public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
    public MyDbContext() : base("MyConnectionString") { }
}
Run Code Online (Sandbox Code Playgroud)

连接字符串正在工作,因为我有其他项目使用相同,他们运行正常.

<connectionStrings>
    <add name="MySailorContext" connectionString="Data Source=THOMAS-LAPTOP;Initial Catalog=MySailor;Integrated Security=True;Pooling=False;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

Rob*_*obM 38

以下是如何成功更改数据库的分步骤.首先,清理你以前可能做过的任何事情.如果您有可能无法获得所有的一切,那么最好是刚开始使用新文件夹中的全新副本.

一旦源100%回到原始状态,确保清除其他所有内容,包括删除"新"数据库和原始数据库(aspnet-AspnetIdentitySample-20130627083537_2).您可以使用SQL Server对象资源管理器找到原始数据库.(Visual Studio中的"查看" - >"SQL Server对象资源管理器")

接下来,在您第一次运行新应用程序之前,请继续进行更改以使用您选择的数据库名称.以下是步骤:


1.在web.config中设置新的数据库连接

  <connectionStrings>
    <!-- Original Connection String -->
    <!--
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;
         Initial Catalog=aspnet-AspnetIdentitySample-20130627083537_2;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    -->
    <!-- New Connection String -->
    <add name="MyConnectionString" connectionString="Data Source=(LocalDb)\v11.0;
         Initial Catalog=MyAspnetIdentitySample_1;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
Run Code Online (Sandbox Code Playgroud)

2.修改AppModel.cs以使DBContext使用新连接:

旧:

    public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
    {
    }
Run Code Online (Sandbox Code Playgroud)

新:

    public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
    {
        public MyDbContext() : base("MyConnectionString") { }
    }
Run Code Online (Sandbox Code Playgroud)

3.启用数据库迁移,创建种子数据并更新以进行验证

3.1-在包管理器控制台中,启用数据库迁移:

PM> enable-migrations
Run Code Online (Sandbox Code Playgroud)

3.2 - 更新Migrations\Configuration.cs以启用自动迁移和种子数据

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(AspnetIdentitySample.Models.MyDbContext context)
    {

        context.Users.AddOrUpdate(
            p => p.UserName,
            new MyUser { UserName = "John Doe" }
        );
    }
Run Code Online (Sandbox Code Playgroud)

3.3 - 通过迁移创建数据库.在程序包管理器控制台中:

PM> update-database
Run Code Online (Sandbox Code Playgroud)

3.4 - 使用SQL Server对象资源管理器并查找数据库"MyAspnetIdentitySample_1",验证是否已创建新数据库(并且最初提供的数据库名称不存在).

4.运行应用程序并创建新登录以进行验证

这就是你如何从头开始成功完成它 - 如果你没有提供更多细节,我无法用/为你解决问题.您应该能够确定出错的地方.

  • 我只是不买这个新的会员技术,MS正在迫使我们的喉咙.过去的Asp.net会员资格非常易于使用.您甚至可以从VS中启动配置网站来创建用户和角色.有了这个废话,我花了一个小时试图弄清楚如何从神奇地自动生成表格的mdf改为真正的SQL服务器. (14认同)

Leo*_*Leo 5

对于新版mvc5应用程序中VS2013附带的1.0版本,答案已无效.现在就这样做:

声明一个类如下:

public class AppUserStore : UserStore<IdentityUser>
{
    public AppUserStore():base(new MY_IDENTITYDBCONTEXT())
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

并在Startup.Auth.cs上更改

UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());    
Run Code Online (Sandbox Code Playgroud)

至

UserManagerFactory = () => new UserManager<IdentityUser>(new AppUserStore());
Run Code Online (Sandbox Code Playgroud)

如果没有这个,你的IdentityDbContext构造函数(OnModelCreating例如)在创建具有asp.net标识的User时将不会运行.