ASP.NET SQL资料提供商 - 请问ProfileBase.Create()方法打DB? [英] ASP.NET SQL Profile Provider - Does the ProfileBase.Create() method hit DB?
问题描述
我的SQLMemebershipProvider工作,并使用配置文件。我有一个叫做用户配置的自定义类从ProfileBase类继承,我用它来设置自定义属性,如全名。我通过数据库中的所有用户希望循环并获得他们的配置文件属性。在每次迭代我打电话ProfileBase.Create()得到一个新的配置文件,然后访问属性。
I am working with the SQLMemebershipProvider and using Profiles. I have a custom class called UserProfile that inherits from the ProfileBase class and I use this to set custom properties like "FullName". I am wanting to loop through all the users in the database and get access to their profile properties. On each iteration I am calling ProfileBase.Create() to get a new profile and then access the properties.
它看起来对我来说,每一次ProfileBase.Create()被调用它击中我的SQL数据库。但我只是寻找证实这一点。因此,没有人知道,如果这样做,其实每次打DB?
It looks to me like every time ProfileBase.Create() is called it hits my SQL database. But I am just looking for confirmation of this. So, does anyone know if this does in fact hit the DB each time?
和更好的是,没有任何人有我怎么可能做一个调用数据库来获得所有用户与他们的自定义配置文件属性更好的解决方案?
And better yet, does anyone have a better solution of how I could make one call to the DB to get all users with their custom profile attributes?
我知道我可以写我自己存储的过程,但我不知道是否有内置的成员资格提供程序的方式。
I know I could write my own stored proc, but I am wondering if there is a way built in to the Membership Provider.
推荐答案
迈克,我相信你观察到的是真实的。我与使用Azure的TableStorage作为数据存储的一个ProfileProvider工作。我想从数据库中获取的用户配置文件的列表,并与成员资格提供的信息合并。
过了一段时间,直到我意识到有一个用户名作为参数调用ProfileBase.Create()执行对TableStorage查找和实际的检索的与该用户名相关的数据。就我而言,调用此方法的创建()的是一种误导,我希望的加载()或获取()的。
目前我的code是这样的:
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
- 检索配置文件提供所有的自定义配置文件信息和
- 合并它们与成员提供的信息告诉他们如在管理员页面?
我有看 Web Profile的构建器但IMO这种只提供设计通过生成的代理类 - 时间智能感知自定义配置文件属性。
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.
这篇关于ASP.NET SQL资料提供商 - 请问ProfileBase.Create()方法打DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!