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

时间:2013-08-18 08:11:43

标签: c# asp.net asp.net-mvc asp.net-identity

我正在使用Asp-Team的ASP.NET Identity Sample,我正在尝试更改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>

7 个答案:

答案 0 :(得分:38)

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

一旦源100%回到原始状态,请确保清除其他所有内容,包括删除“新”数据库和原始数据库(aspnet-AspnetIdentitySample-20130627083537_2)。您可以使用SQL Server对象资源管理器找到原始数据库。 (“查看” - &gt; 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使用新连接:

OLD:

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

NEW:

    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。运行应用并创建新的登录以验证

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

答案 1 :(得分: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标识的用户时将无法运行。

答案 2 :(得分:2)

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

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

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

我的模型类继承了DbContext。希望这会有所帮助。

答案 3 :(得分: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") { }
}

答案 4 :(得分: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";

答案 5 :(得分:1)

由于在转到RTM时有很多变化,我已经更新了使用WebApi控制器进行所有身份登录的SPA模板等。它是一个非常酷的模板,如果你还没有看到它。

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

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

答案 6 :(得分:1)

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

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

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