gpt4 book ai didi

ASP.NET SQL Profile Provider - ProfileBase.Create() 方法是否命中 DB?

转载 作者:行者123 更新时间:2023-12-01 01:37:14 24 4
gpt4 key购买 nike

我正在使用 SQLMemebershipProvider 并使用配置文件。我有一个名为 UserProfile 的自定义类,它继承自 ProfileBase 类,我使用它来设置自定义属性,如“FullName”。我想遍历数据库中的所有用户并访问他们的配置文件属性。在每次迭代中,我都会调用 ProfileBase.Create() 来获取新的配置文件,然后访问属性。

在我看来,每次调用 ProfileBase.Create() 时它都会命中我的 SQL 数据库。但我只是想确认这一点。那么,有谁知道这是否真的每次都击中了数据库?

更好的是,有没有人有更好的解决方案来说明我如何向数据库发出一次调用以获取所有用户的自定义配置文件属性?

我知道我可以编写自己的存储过程,但我想知道是否有一种内置到成员(member)提供程序的方法。

最佳答案

迈克,我相信你所观察到的是真的。我正在使用使用 Azure TableStorage 作为数据存储的 ProfileProvider。我想从数据库中获取用户配置文件列表,并将它们与成员(member)提供商提供的信息合并。
我花了一些时间才意识到使用用户名作为参数调用 ProfileBase.Create() 会执行对 TableStorage 的查找并实际检索与该用户名关联的数据。就我而言,调用此方法 Create() 具有误导性,我希望 Load() 或 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;
}

有没有更好(更直接、更高效)的方法
  • 从配置文件提供程序和
  • 检索所有自定义配置文件信息
  • 将它们与成员(member)提供商信息合并以显示它们,例如在管理员页面?

  • 我看过 Web Profile Builder但 IMO 这仅通过生成代理类为自定义配置文件属性提供设计时智能感知。

    关于ASP.NET SQL Profile Provider - ProfileBase.Create() 方法是否命中 DB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1126719/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com