4

この記事のように、データベース テーブルとデータ (ユーザー、ロール内のユーザーなど) の初期化をConfiguration.csに移動したいと思います。and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

しかし、私は質問があります: ユーザーを追加するための UserId を設定することは可能ですか? つまり、5 人のユーザーを追加でき、私が思うに、1-2-3-4-5 の ID を取得する必要がありますが、それを制御して ID マニュアルを設定できますか?

初期化は次のようになります。

        if (!WebSecurity.UserExists("elena"))
            WebSecurity.CreateUserAndAccount("elena", "password");
4

1 に答える 1

10

はい、これを行うには、設定するプロパティを渡します。例えば:

WebSecurity.CreateUserAndAccount("elena", "password", new { UserId = 1 });

もちろん、UserId 列が ID 列に設定されていないことを確認する必要があり、UsersContext スキーマを変更してそれを追加する必要もあります。

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserId { get; set; }
    public string UserName { get; set; }
}
于 2012-10-14T20:45:12.973 に答える