ASP.NET SQL Profile Provider - Does the ProfileBase.Create() method hit DB?

爱⌒轻易说出口 提交于 2019-12-01 07:38:27

Mike, I believe what you observed is true. I am working with a ProfileProvider that uses Azure TableStorage as data store. I wanted to get a list of user profiles from database and merge them with information from membership provider. It took some time until I realized that calling ProfileBase.Create() with a username as argument performs a lookup against TableStorage and actually retrieves the data associated with that username. As far as I'm concerned, calling this method Create() is misleading, I would expect Load() or Get(). Currently my code looks like this:

    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;
    }

Is there a better (more straightforward, more performant) way to

  1. retrieve all the custom profile information from profile provider and
  2. merge them with membership provider info to show them e.g. in an administrator page?

I have had a look at Web Profile Builder but IMO this only provides design-time intellisense for custom profile properties by generating a proxy class.

You don't persist to the database until you call Save:

The Save method writes modified profile property values to the data source. The profile provider can reduce the amount of activity at the data source by performing updates only when the IsDirty property is set to true. This is the case for the default SqlProfileProvider.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!