Entity Framework コードファーストの問題 (SimpleMembership UserProfile テーブル) 質問する

Entity Framework コードファーストの問題 (SimpleMembership UserProfile テーブル) 質問する

ASP.NET MVC 4 を使用したことがある場合は、インターネット アプリケーションのデフォルトでは SimpleMembership プロバイダーが使用されることに気付くでしょう。これはすべて問題なく、正常に動作します。

UserProfileこの問題はデフォルトのデータベース生成に関係しており、次のように定義された POCO があります。

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
}

.. 次のように生成されます:

using (var context = new UsersContext())
{
    if (!context.Database.Exists())
    {
         // Create the SimpleMembership database without Entity Framework migration schema
         ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
    }
}

これは正常に動作し、データベースは正常に生成され、問題なく動作します。ただし、POCO を次のように変更してデータベースを削除すると、

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string EmailAddress { get; set; }

    public string FirstName { get; set; }
    public string Surname { get; set; }

    public string Country { get; set; }

    public string CompanyName { get; set; }
}

最初の 2 列のみが生成されますUserIdEmailAddressコード的には問題なく動作しますが (ログイン/登録について)、他のユーザー データは明らかに保存されません。

何か見落としているのでしょうか? 確かに、オブジェクト全体に基づいてデータベースを生成する必要がありますUserProfile

ベストアンサー1

1 - 移行を有効にする必要があります。EntityFramework 5 を使用することをお勧めします。NuGetEnable-Migrationsパッケージ マネージャーで使用します。

2 - 移動する

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "EmailAddress", autoCreateTables: true); 

YourMvcApp/Migrations/Configuration.csクラスのSeedメソッドに

    protected override void Seed(UsersContext context)
    {
        WebSecurity.InitializeDatabaseConnection(
            "DefaultConnection",
            "UserProfile",
            "UserId",
            "UserName", autoCreateTables: true);

        if (!Roles.RoleExists("Administrator"))
            Roles.CreateRole("Administrator");

        if (!WebSecurity.UserExists("lelong37"))
            WebSecurity.CreateUserAndAccount(
                "lelong37",
                "password",
                new {Mobile = "+19725000000", IsSmsVerified = false});

        if (!Roles.GetRolesForUser("lelong37").Contains("Administrator"))
            Roles.AddUsersToRoles(new[] {"lelong37"}, new[] {"Administrator"});
    }

これで、EF5 が UserProfile テーブルの作成を担当するようになります。その後、WebSecurity.InitializeDatabaseConnection を呼び出して、既に作成されている UserProfile テーブルに SimpleMembershipProvider のみを登録し、どの列が UserId と UserName であるかを SimpleMembershipProvider に伝えます。また、ユーザーとロールを追加し、Seed メソッドでこれら 2 つをカスタム UserProfile プロパティ/フィールド (ユーザーの Mobile (番号) など) に関連付ける方法の例も示します。

3 - パッケージ マネージャー コンソールから update-database を実行すると、EF5 はすべてのカスタム プロパティを使用してテーブルをプロビジョニングします。

追加の参考資料については、ソースコード付きのこの記事を参照してください。http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

おすすめ記事