2013-05-11 48 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