0

我正在尝试使用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一切都很好。

我在这里做错了什么?

4

1 回答 1

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它与您所做的完全相同,但您不必在代码中指定确切的提供程序。

于 2013-05-12T23:13:29.510 回答