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

.net asp.net-mvc entity-framework

Question

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");
1
4
10/14/2012 8:39:41 PM

Accepted Answer

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; }
}
10
10/14/2012 8:45:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow