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

I would like to move my initialization of tables and database data (user, user in role, etc.) to Configuration.cs, as 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 configure UserId to add users? I mean, I could add 5 users, and I think they should get 1-2-3-4-5 identifiers, but can I control it and set up their identifier guide?

Initialization now looks like this:

if (!WebSecurity.UserExists("elena")) WebSecurity.CreateUserAndAccount("elena", "password"); 
+4
source share
1 answer

Yes, you would do this by passing properties to set. For instance:

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

Of course, you need to make sure that the UserId column has not been set as the identifier column, and you will also have to modify the UsersContext schema to add this.

 [Table("UserProfile")] public class UserProfile { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int UserId { get; set; } public string UserName { get; set; } } 
+10
source

Source: https://habr.com/ru/post/1439693/


All Articles