【问题标题】:ASP.Net Identity how to set target DB?ASP.Net Identity 如何设置目标数据库?
【发布时间】:2013-08-18 08:11:42
【问题描述】:

我正在使用来自 Asp 团队的 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>

【问题讨论】:

  • 1) 你到底想做什么? 2) 在对代码进行任何更改(包括连接字符串)之前,您是否能够在应用程序中成功创建新用户? 3) 到目前为止,您对应用程序进行了哪些更改? 4) 更改连接字符串后,是否启用迁移并更新数据库以生成/更新架构?
  • 2) 是的,它工作成功 3) 只更改了连接字符串和 DbContext 构造函数 4) 是的,我做了,否则我得到一个 SqlException
  • 好的,为您的更改提供一些上下文,包括“public MyDbContext...”。仍然没有上面#1的答案 - 你想做什么?如果只是更改所有内容的 db,为什么不更改 web.config 中现有的连接字符串?
  • @RobM 这正是我所做的唯一一件事......
  • 我想帮助你,但你需要提供更多信息!文件名、上下文 sn-p 包括周围的行...用于连接字符串的 web.config 部分。

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


【解决方案1】:

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

一旦源 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.运行应用并创建一个新的登录来验证

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

【讨论】:

  • 我正在使用代码优先,EF模型已经成功运行,我今天想添加登录的东西,但它不工作......
  • 谢谢你,但同样,它不是数据库错误......上下文连接到数据库,它只是无缘无故地返回false......任何异常都会重新抛出给我...... .
  • 我只是不购买所有这些 MS 强加给我们的新会员技术。过去的 Asp.net Membership 非常容易开箱即用。您甚至可以从 VS 中启动配置网站来创建用户和角色。有了这个废话,我花了一个小时试图弄清楚如何从神奇地自动生成表的 mdf 更改为真正的 sql 服务器。
  • 为了搜索者的利益,出于某种原因,我无法停止我的项目查看 localdb。 update-database -verbose 上的错误是“目标数据库是:'DefaultConnection'(数据源:(localdb)\mssqllocaldb”。这似乎是修复。
【解决方案2】:

对于新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 身份的用户时将不会运行。

【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 这就是我在这两部分上所做的更改,但得到错误建立与 SQL 的连接时发生与网络相关或特定于实例的错误。
      【解决方案5】:

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

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

      【讨论】:

      • 只是对上面评论的注释。你永远不应该把你的连接字符串放在代码中,作为一个好的做法,你应该把它放在一个 appConfig 文件中,并从那里在任何需要它的地方使用它。 System.Configuration.ConfigurationManager.AppSettings[key];
      【解决方案6】:

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

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

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

      【讨论】:

        【解决方案7】:

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

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

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

        【讨论】:

        • 不工作,我们是否需要更改更多地方,更改 web.config 和 applicationDbContext 对我来说还不够。
        猜你喜欢
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        • 2016-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多