加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP .NET MVC 4:WebSecurity.CreateUserAndAccount如何设置User

发布时间:2020-12-16 07:28:27 所属栏目:asp.Net 来源:网络整理
导读:我想在Configuration.cs中移动数据库表和数据(用户,角色e.t.c.中的用户)的初始化,如本文所述: http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properti
我想在Configuration.cs中移动数据库表和数据(用户,角色e.t.c.中的用户)的初始化,如本文所述: http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

但我有一个问题:是否可以设置添加用户的UserId?我的意思是,我可以添加5个用户,并且,我认为他们应该得到1-2-3-4-5 Ids,但我可以控制它并设置他们的Ids手册吗?

现在初始化看起来像:

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

解决方法

是的,您可以通过传入要设置的属性来执行此操作.例如:

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

当然,您需要确保UserId列未设置为标识列,并且您必须更改UsersContext架构以添加它.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读