2009-07-14 73 views
4

我正在使用SQLMemebershipProvider並使用配置文件。我有一個名爲UserProfile的自定義類,它繼承自ProfileBase類,我使用它來設置自定義屬性,如「FullName」。我想循環訪問數據庫中的所有用戶並訪問其配置文件屬性。在每次迭代時,我都調用ProfileBase.Create()來獲取新配置文件,然後訪問屬性。ASP.NET SQL配置文件提供程序 - ProfileBase.Create()方法是否碰到數據庫?

它看起來像每次ProfileBase.Create()被稱爲它擊中我的SQL數據庫。但我只是在尋求確認。那麼,有沒有人知道這是否確實每次都碰到數據庫?

更好的是,有沒有人有更好的解決方案,我可以打電話給數據庫以獲得所有用戶的自定義配置文件屬性?

我知道我可以編寫自己的存儲過程,但我想知道是否有內置到成員資格提供程序的方法。

回答

4

邁克,我相信你觀察到的是真的。我正在使用使用Azure TableStorage作爲數據存儲的ProfileProvider。我想從數據庫中獲取用戶配置文件的列表,並將它們與來自成員資格提供者的信息合併。 花了一些時間,直到我意識到以用戶名作爲參數調用ProfileBase.Create()執行對TableStorage的查找並且實際上檢索到與該用戶名相關聯的數據。就我而言,調用這個方法Create()是誤導,我期望加載()Get()。 目前我的代碼看起來是這樣的:

public IEnumerable<AggregatedUser> GetAllAggregatedUsers() 
    { 
     ProfileInfoCollection allProfiles = this.GetAllUsersCore(
      ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All) 
     ); 

     //AggregatedUser is simply a custom Class that holds all the properties (Email, FirstName) that are being used 
     var allUsers = new List<AggregatedUser>(); 

     AggregatedUser currentUser = null; 
     MembershipUser currentMember = null; 
     foreach (ProfileInfo profile in allProfiles) 
     { 
      currentUser = null; 
      // Fetch profile information from profile store 
      ProfileBase webProfile = ProfileBase.Create(profile.UserName); 
      // Fetch core information from membership store 
      currentMember = Membership.FindUsersByName(profile.UserName)[profile.UserName]; 
      if (currentMember == null) 
       continue; 

      currentUser = new AggregatedUser(); 
      currentUser.Email = currentMember.Email; 
      currentUser.FirstName = GetStringValue(webProfile, "FirstName"); 
      currentUser.LastName = GetStringValue(webProfile, "LastName"); 
      currentUser.Roles = Roles.GetRolesForUser(profile.UserName); 
      currentUser.Username = profile.UserName; 
      allUsers.Add(currentUser); 
     } 

     return allUsers; 
    } 

    private String GetStringValue(ProfileBase profile, String valueName) 
    { 
     if (profile == null) 
      return String.Empty; 
     var propValue = profile.PropertyValues[valueName]; 
     if (propValue == null) 
      return String.Empty; 

     return propValue.PropertyValue as String; 
    } 

有沒有更好的(更簡單的,更高性能的)的方式來

  1. 檢索配置文件提供所有的自定義配置文件信息,並
  2. 與它們合併會員提供者信息以顯示他們在管理員頁面?

我看過Web Profile Builder,但IMO僅通過生成代理類爲定製配置文件屬性提供設計時智能感知。

0

你不堅持到數據庫中,直到您叫Save

Save方法寫入修改 配置文件屬性值的數據 源。當IsDirty屬性設置爲 爲true時,配置文件提供程序可以通過僅執行更新 來減少 數據源上的活動數量,以便 可以減少活動數量。這是默認的 SqlProfileProvider的情況。

+0

我沒有調用Save方法。我只是試圖檢索配置文件及其自定義屬性。 – 2009-07-15 16:18:17

+0

順便說一句,如果你設置AutoSave = true,那麼你的配置文件將自動更新 – EvgeniyK 2013-06-19 11:01:08

相關問題