1

我调用 CreateUserAndAccount 方法在用户名和密码中提供字符串。但我得到一个例外,说硬编码的字符串为空。这怎么可能!?!我在这里错过了什么?

代码:WebSecurity.CreateUserAndAccount("Admin", "Admin", null, false);

SQLEXCEPTION:无法将值 NULL 插入列 'UserPwd'、表 'servidb.dbo.User';列不允许空值。插入失败。

我也将表的名称从 UserProfile 更改为 User 和 Password 列。可能是这里的错误?

问候。

ps:我不知道还有什么代码要显示。如果需要更多信息,请询问。

编辑:其实我做到了:

WebSecurity.InitializeDatabaseConnection("connectionString", "User", "UserId", "UserName", autoCreateTables: true);

我检查了数据库,并使用该列创建了表用户。我的实体代码是这个

[Table("User")]
public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    [Required]
    public string UserName { get; set; }
    [Required]
    public string UserPwd { get; set; }
}
4

2 回答 2

1

尝试这个:

WebSecurity.CreateUserAndAccount("Admin", "Admin", new {UserPwd = ""}, false)

第三个参数代表一个匿名对象,您可以使用它来提供您的Required字段,以便您的 SQL 获取它需要的所有非空值

于 2014-08-08T16:34:37.350 回答
0

这可能是因为您更改了这些列名,WebSecurity 知道自己要使用哪些列,它实际上使用自己生成的表,除了您的带有用户名的表。如果您使用 Internet 模板,它可能在您的 InitializeSimplemembership.cs 中。

WebSecurity.InitializeDatabaseConnection([here database username],
[here database password], [here YOUR talbe name containing user name column], 
[here user name column] , autoCreateTables: true);

因此,您只创建了一个带有 UserName 列的表,仅此而已,其余的由 WebSecurity 在新表中创建。

于 2013-08-23T02:05:13.477 回答