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

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

我用构造函数试了一下

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

和DbContext类一样。

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

 await IdentityStore.CreateLocalUser(user, model.Password) 

返回false …没有错误,没有。

任何想法如何解决?

编辑:

Filename是Models\AppModel.cs ,还有MyDbContext class

原来是

 public class MyDbContext : IdentityDbContext { } 

我改成了

 public class MyDbContext : IdentityDbContext { public MyDbContext() : base("MyConnectionString") { } } 

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

    

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

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

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


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

        

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

旧:

  public class MyDbContext : IdentityDbContext { } 

新:

  public class MyDbContext : IdentityDbContext { public MyDbContext() : base("MyConnectionString") { } } 

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

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”,validation是否已创建新数据库(并且最初提供的数据库名称不存在)。

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

这就是你如何从头开始成功完成它 – 如果你没有提供更多的细节,我不能用/为你排除故障。 您应该能够确定出错的地方。

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

声明一个类如下:

 public class AppUserStore : UserStore { public AppUserStore():base(new MY_IDENTITYDBCONTEXT()) { } } 

并在Startup.Auth.cs上更改

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

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

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

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

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

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

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

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

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

    public class MyDbContext : IdentityDbContext { public MyDbContext() : base("MyConnectionString") { } } 

无论你给什么名字,你的connectionString必须匹配你在DbContext:base中的任何东西

    public class MyDbContext : IdentityDbContext { public MyDbContext() : base("MySailorContext") { } } 

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

 //Leave this untouched: IdentityManager = new AuthenticationIdentityManager(new IdentityStore()); //...and add this: ((IdentityStore)identityManager.Store).DbContext.Configuration .Database.Connection.ConnectionString = "your connection string here"; 

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

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

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

查找从IdentityDbConectinheritance的类,例如:

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

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