Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP .NET MVC 4: WebSecurity.CreateUserAndAccount how to set UserId

I'd like to move my initialization of database tables and data (user, user in role e.t.c.) in Configuration.cs like in this article: http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

But i have a question: is it possible to set up UserId of adding users? I mean, i could add 5 users, and, as i think, they should get 1-2-3-4-5 Ids, but could i controll it and set up their Ids manual?

Now initialization looks like:

        if (!WebSecurity.UserExists("elena"))
            WebSecurity.CreateUserAndAccount("elena", "password");
like image 668
user1612334 Avatar asked Dec 26 '22 16:12

user1612334


1 Answers

Yes, you would do this by passing in the properties to set. For example:

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

Of course, you would need to make sure the UserId column was not set to be an identity column, and you would have to change your UsersContext schema to add that as well.

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserId { get; set; }
    public string UserName { get; set; }
}
like image 61
Erik Funkenbusch Avatar answered May 09 '23 19:05

Erik Funkenbusch