2013-05-11 99 views
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一切顺利。

我在这里做错了什么?

回答

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不完全一样你正在做,但是你不必在你的代码中指定确切的提供者。

+0

嗨,有道理,我想我也尝试过这个选项,但不知道。但是,对于websecurity来说,你指的是什么,不能指定确切的提供者? – Yustme 2013-05-13 09:07:46

+0

@Yustme。它只是将你从具体的提供者实现中抽象出来。它的代码还需要'Membership.Provider',但是将它转换为所有提供者的公共基类,而不是'SimpleMembershipProvider'(对IoC和/或DI来说可能更好)。除非你对性能超级担心,否则我会使用WebSecurity。 – 2013-05-13 09:10:43

+0

好的,谢谢! – Yustme 2013-05-13 12:20:32