2012-10-14 44 views
4

我想將我在Configuration.cs數據庫表和數據(用戶,用戶角色等)的初始化喜歡這篇文章中:http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/ASP .NET MVC 4:WebSecurity.CreateUserAndAccount如何設置用戶ID

但我有一個問題:是否有可能設置UserId添加用戶?我的意思是,我可以添加5個用戶,並且,我認爲他們應該獲得1-2-3-4-5個ID,但是我可以控制它並設置他們的ID手冊嗎?

現在初始化的樣子:

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

回答

10

是的,你會被傳遞到設置的屬性做到這一點。例如:

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

當然,你需要確保用戶ID列並沒有被設置爲標識列,你必須改變你的UsersContext架構以添加這一點。

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

我試圖用新的用戶名{= 2}做它,但它並沒有幫助 - 將創建一個用戶id = 1。這意味着,真的,我沒有在任何情況下首先控制研究用戶 – user1612334

+0

@ user1612334 - 查看我的更新。 –

+0

好的,thx ......... – user1612334