我正在尝试使用SimpleMembershipProvider
为一些用户播种。我在UserProfile表中添加了几个列,就像手机号码一样。当我尝试添加具有手机号码的用户时,编译器会告诉我:
The name 'Mobile' does not exist in the current context
这是班级:
namespace _DataContext.Migrations {
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using WebMatrix.WebData;
using System.Web.Security;
internal sealed class Configuration : DbMigrationsConfiguration<_DataContext.DataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(_DataContext.DataContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
SeedMembership();
}
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
var roles = (SimpleRoleProvider)Roles.Provider;
var membership = (SimpleMembershipProvider)System.Web.Security.Membership.Provider;
if (!roles.RoleExists("Administrator"))
roles.CreateRole("Administrator");
if (membership.GetUser("Username", false) == null)
membership.CreateUserAndAccount("Username", "Pass", false,
new Dictionary<string, object>
{
{ Mobile = "+311122334455" },
});
/*if (!WebSecurity.UserExists("test"))
WebSecurity.CreateUserAndAccount(
"Username",
"password",
new {
Mobile = "+311122334455",
FirstName = "test",
LastName = "test",
LoginCount = 0,
IsActive = true,
});
*/
}
}
}
如果我使用WebSecurity
一切正常。
我在这里做错了什么?
答案 0 :(得分:1)
这就是您创建Dictionary
的方式,您无法做到:
membership.CreateUserAndAccount("Username", "Pass", false,
new Dictionary<string, object>
{
{ Mobile = "+311122334455" }, // Mobile won't compile here
});
所以改为使用:
membership.CreateUserAndAccount("Username", "Pass", false,
new Dictionary<string, object>
{
{ "Mobile", "+311122334455" }, // Mobile should be the string in the string, object pair
});
对于它的价值,WebSecurity
与您正在做的完全相同,但是您必须在代码中指定确切的提供程序。