Asp.net mvc 4 使用成员资格提供程序为用户设定种子

Asp.net mvc 4 使用成员资格提供程序为用户设定种子,asp.net-mvc-4,ef-code-first,entity-framework-migrations,Asp.net Mvc 4,Ef Code First,Entity Framework Migrations,我正在尝试使用SimpleMembershipProvider为一些用户植入种子。我在UserProfile表中添加了一些列,比如手机号码。当我尝试添加带有手机号码的用户时,编译器会告诉我: The name 'Mobile' does not exist in the current context 这是一节课: namespace _DataContext.Migrations { using System; using System.Data.Entity; u

我正在尝试使用
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,
                        });
              */
    }
  }
}
namespace\u DataContext.Migrations{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Migrations;
使用System.Linq;
使用WebMatrix.WebData;
使用System.Web.Security;
内部密封类配置:DBMigOptionsConfiguration
{
公共配置()
{
AutomaticMiggerationsEnabled=真;
}
受保护的重写无效种子(_DataContext.DataContext)
{
//迁移到最新版本后将调用此方法。
//您可以使用DbSet.AddOrUpdate()助手扩展方法
//避免创建重复的种子数据。
//
//context.People.AddOrUpdate(
//p=>p.FullName,
//新人{FullName=“安德鲁·彼得斯”},
//新人{FullName=“Brice Lambson”},
//新人{FullName=“Rowan Miller”}
//    );
//
种子成员资格();
}
私人会员资格()
{
WebSecurity.InitializeDatabaseConnection(“DefaultConnection”、“UserProfile”、“UserId”、“UserName”,autoCreateTables:true);
var roles=(SimpleRoleProvider)roles.Provider;
var membership=(SimpleMembershipProvider)System.Web.Security.membership.Provider;
如果(!roles.RoleExists(“管理员”))
角色。创建角色(“管理员”);
if(membership.GetUser(“Username”,false)==null)
CreateUserAndAccount(“用户名”,“通过”,false,
新词典
{ 
{Mobile=“+31112334455”},
});
/*如果(!WebSecurity.UserExists(“test”))
WebSecurity.CreateUserAndAccount(
“用户名”,
“密码”,
新{
Mobile=“+31112334455”,
FirstName=“test”,
LastName=“测试”,
LoginCount=0,
IsActive=true,
});
*/
}
}
}
如果我使用
WebSecurity
一切正常


我做错了什么?

这只是您创建
词典的方式,您不能:

membership.CreateUserAndAccount("Username", "Pass", false,
    new Dictionary<string, object> 
    { 
        { Mobile = "+311122334455" }, // Mobile won't compile here
    });
membership.CreateUserAndAccount(“用户名”,“通过”,false,
新词典
{ 
{Mobile=“+31112334455”},//Mobile不会在这里编译
});
因此,请使用:

membership.CreateUserAndAccount("Username", "Pass", false,
    new Dictionary<string, object> 
    { 
        { "Mobile", "+311122334455" }, // Mobile should be the string in the string, object pair
    });
membership.CreateUserAndAccount(“用户名”,“通过”,false,
新词典
{ 
{“Mobile”,“+31112334455”},//Mobile应该是字符串、对象对中的字符串
});

值得一提的是,
WebSecurity
的功能与您现在所做的完全相同,但您不必在代码中指定确切的提供者。

您好,有道理,我想我也尝试过这个选项,但不确定。但是你说的网络安全是什么意思阻止我指定确切的提供者?@Yustme。它只是将您从具体的提供者实现中抽象出来。它的代码还接受
成员资格.Provider
,但将其强制转换为所有提供者的公共基类,而不是
SimpleMembershipProvider
(因此对于IoC和/或DI可能更好)。除非你非常担心性能,否则我会使用WebSecurity。