23

Asp-TeamのASP.NET Identity Sampleを使用しており、データベースを変更しようとしていますIdentityDbContext...

コンストラクタでやってみた

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

DbContext クラスと同様です。

ユーザーを登録しようとするまではうまくいきます...

await IdentityStore.CreateLocalUser(user, model.Password)

戻り値false... エラーなし、何もありません。

それを修正する方法はありますか?

編集:

ファイル名はModels\AppModel.csMyDbContext 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());

OnModelCreatingこれがないと、asp.net ID を使用してユーザーを作成するときに、IdentityDbContext コンストラクター (およびなど) が実行されません。

于 2013-11-04T09:56:04.023 に答える
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 にあるものと一致する必要があります。

<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 に答える
2

私は同じ問題を抱えていて、ASP.NET Identity に関するネット上の資料がまだあまりないため、少し苦労しました。アセンブリをチェックした後、実際には簡単であることがわかりました。

AuthenticationIdentityManager が初期化されている場所を見つけて、次のように IdentityStore オーバーロードを使用して db コンテキストを指定するだけです。

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

私のモデル クラスは DbContext を継承しています。お役に立てれば。

于 2013-09-14T03:02:17.197 に答える
1

RTM に移行する際にこれに多くの変更があるため、すべての ID サインインなどに WebApi コントローラーを使用する SPA テンプレートを更新しました。あなたがそれを見たことがないなら、それは本当にクールなテンプレートです。

すべてのコードをここに置きます: https://github.com/s093294/aspnet-identity-rtm/tree/master

(注意してください、それはインスピレーションのためだけです。私はそれを機能させただけで、それ以上のものはありません。適切にバグも1つか2つ持っています)。

于 2013-10-04T00:24:51.463 に答える