23

我正在使用来自 Asp-Team 的ASP.NET 身份示例,并且我正在尝试更改数据库以用于IdentityDbContext...

我用构造函数试过了

public MyDbContext() : base("MyConnectionString") { } // base is IdentityDbContext

就像使用 DbContext 类一样。

在我尝试注册用户之前效果很好......

await IdentityStore.CreateLocalUser(user, model.Password)

返回false...没有错误,什么都没有。

任何想法如何解决这个问题?

编辑:

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

原来是

public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
}

我把它改成

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

连接字符串正在工作,因为我有其他项目使用相同的,并且它们运行良好。

<connectionStrings>
    <add name="MySailorContext" connectionString="Data Source=THOMAS-LAPTOP;Initial Catalog=MySailor;Integrated Security=True;Pooling=False;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
4

7 回答 7

41

以下是有关如何成功更改数据库的分步说明。首先,清理你以前可能做过的任何事情。如果有任何机会您可能无法获得所有内容,那么最好从新文件夹中的全新副本开始。

一旦源 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>

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

老的:

    public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
    {
    }

新的:

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

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

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

PM> enable-migrations

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

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

PM> update-database

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

4.运行应用程序并创建一个新的登录来验证

这就是您可以从头开始成功完成此操作的方法——如果您不提供更多详细信息,我无法与您一起/为您排除故障。您应该能够确定哪里出错了。

于 2013-08-18T08:41:34.847 回答
5

对于新的mvc5应用程序上VS2013附带的1.0版本,答案不再有效。现在执行此操作:

声明一个类,如:

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

    }
}

并在 Startup.Auth.cs 更改

UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());    

UserManagerFactory = () => new UserManager<IdentityUser>(new AppUserStore());

如果没有这个,您的 IdentityDbContext 构造函数(OnModelCreating例如)在创建具有 asp.net 身份的用户时将不会运行。

于 2013-11-04T09:56:04.023 回答
2

我遇到了同样的问题并且有点挣扎,因为网络中关于 ASP.NET Identity 的资料还不是很多。在对程序集进行检查后,我发现它实际上很简单。

只需找到初始化 AuthenticationIdentityManager 的位置并使用 IdentityStore 重载来指定您的数据库上下文,如下所示:

IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new Model()));

我的模型类继承了 DbContext。希望这可以帮助。

于 2013-09-14T03:02:17.197 回答
2

RobM 有全面的答案,而 Rob B 有简单的答案。

在您的场景中,您将基础设置为配置文件中不存在的“MyConnectionString”。

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

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

无论您给 connectionString 提供什么名称,都必须与您在 DbContext 中的任何名称相匹配:base

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

public class MyDbContext : IdentityDbContext<MyUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
    public MyDbContext() : base("MySailorContext") { }
}
于 2014-05-16T13:07:26.000 回答
1

如果您只需要更改身份提供者使用的连接字符串,则以下内容不是很优雅,但似乎可行。搜索 IdentityManager 类的实例化位置并添加以下内容:

//Leave this untouched:
IdentityManager = new AuthenticationIdentityManager(new IdentityStore());
//...and add this:
((IdentityStore)identityManager.Store).DbContext.Configuration
    .Database.Connection.ConnectionString = "your connection string here";
于 2013-09-21T10:24:34.683 回答
1

由于在转至 RTM 时对此有很多更改,因此我更新了 SPA 模板,该模板使用 WebApi 控制器进行所有身份登录等。它是一个非常酷的模板,如果你还没有看过的话。

我把我所有的代码都放在这里: https ://github.com/s093294/aspnet-identity-rtm/tree/master

(请注意,它只是为了灵感。我只是让它工作而已。适当地也有一两个错误)。

于 2013-10-04T00:24:51.463 回答
1

查找从 IdentityDbConect 继承的类,例如:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    }

然后将“DefaultConnection”更改为 web.config 中标识的连接字符串的名称。

于 2014-02-02T18:44:24.200 回答