2013-07-01 11 views
5

新しいASP.NET MVC 5 Previewがリリースされました。どのようにUsersコンテキスト/テーブルを設定しますか?ユーザーコンテキスト/テーブルを設定するにはどうすればよいですか?

MVC 4で、私はちょうど私の自身のUserクラスを使用して、WebSecurity社がそれに初期化指すことになり、これをタイク:

WebSecurity.InitializeDatabaseConnection(connectionString, "System.Data.SqlClient", userTableName, userIdColumn, userNameColumn, autoCreateTables); 

は、私はユーザークラスに追加のプロパティを追加する - どのように?

答えて

1

を実行する手順は、簡単にEFベースの実装を行うことがありますが、あなたは常にドロップダウンと独自のカスタムIUserStoreを実装し、自分自身に渡すことができますDbContext。

私は、必要に応じてより詳細な例を提供することができます。

+0

私は本当に例を見たいと思います。私は自分自身を実装しようとしましたが、いくつかの主張のアイデンティティの問題で終わって、AntiFogeryTokenが失敗する原因となりました。自分のユーザークラスにMicrosoft.AspNet.Identity.IUserを使用すると発生します。 – Martin

+0

@Hao Kung、私はトップダウンの例を見たいと思います。私はまた、 "MyUsers"の新しいテーブルを避けたい。この例でカスタムフィールドを持つロールを含めてください。 – KidCoke

+0

@Hao Kung、あなたは例を挙げますか? – RezaRahmati

1

https://github.com/rustd/AspnetIdentitySampleからサンプルをダウンロードできます。これは、ASP.NET and Web Tools 2013 Preview Refresh(英語版のVS2013プレビューのみをサポート)に同梱されているASP.NET MVCテンプレートに基づいています。このプレビュー更新がインストールされると、ASP.NET WebフォームおよびSPAアプリケーションに対しても同じことができます。続き

はUSERSTOREとユーザークラスこのプロジェクト

Open the solution 
Build and run 
Register a user ---- Notice that the user registration field only has user name and password 
Let's ask for a birthdate option from the user while registering an account. 
Goto Nuget Package Manager console and run "Enable-Migrations" 
Goto Models\AppModel.cs and uncomment BirthDate property in the MyUser class 
Goto Models\AccountViewModels.cs and uncomment BirthDate property in RegisterViewModel 
Goto AccountController and in Register Action and have the following code var user = new MyUser() { UserName = model.UserName,BirthDate=model.BirthDate }; //var user = new MyUser() { UserName = model.UserName }; 
Goto Views\Account\Register.cshtml and uncomment the HTML markup to add a BirthDate column 
Goto Nuget Package Manager console and run "Add-Migration BirthDate" 
Goto Nuget Package Manager console and run "Update-Database" 
Run the application 
When you register a user then you can enter BirthDate as well 
+0

私は既にこの例を見ていますが、MyUserを作成しています。私はEntity Frameworksユーザークラスではなく、自分自身のUserクラスが本当に必要です。したがって、上記のソリューションを使用すると、データベースにMyUsersテーブルが作成されます。これは望ましくありません。 – Martin

2

私が思うに、これはあなたの問題を解決することができます:

モデルで

は、あなた自身のUserモデルを再定義することができます IdentityModels.csを\:

public class ApplicationUser : IdentityUser 
{ 
    /* identity field from database */ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 

    [Required] 
    public bool Internal { get; set; } 

    public string UserFullName { get; set; } 

    public string UserEmail { get; set; } 

    public ApplicationUser() 
     : base() 
    { 
     Internal = false; 
    } 

    public ApplicationUser(string userName) 
     : base(userName) 
    { 
     Internal = false; 
    } 

} 

今はデフォルトのマッピングを変更することができますを使用しているAspNetテーブルOnModelCreating()オーバーライドとToTable()メソッド:

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 

     // Change the name of the table to be Users instead of AspNetUsers 
     modelBuilder.Entity<IdentityUser>().ToTable("User"); 
     modelBuilder.Entity<ApplicationUser>().ToTable("User"); 

     modelBuilder.Entity<IdentityRole>().ToTable("Role"); 
     modelBuilder.Entity<IdentityUserClaim>().ToTable("User_Claim"); 
     modelBuilder.Entity<IdentityUserLogin>().ToTable("User_Login"); 
     modelBuilder.Entity<IdentityUserRole>().ToTable("User_Role"); 
    } 
} 

最後に、データベースを以下の表に表示されます。 ユーザー、役割、USER_ROLE、User_Claim、代わりにAspNetUsers、AspNetRoles、AspNetUsersRoles、AspNetUsersClaims、AspNetUserLoginsのUSER_LOGINユーザーID(int型の同一性)、内部UserFullNameUSEREMAIL

もちろんユーザーテーブルには、追加のフィールドが含まれます。

関連する問題