我最近自己做了一些非常相似的事情,假设你的 Member 类看起来像下面这样(或者至少做同样的事情)。
public class MemberProfile : ProfileBase
{
#region Firstname
private const string FIRSTNAME = "_firstname";
[SettingsAllowAnonymous(false)]
public string FirstName
{
get
{
return GetCustomProperty(FIRSTNAME);
}
set
{
SetCustomProperty(FIRSTNAME, value);
}
}
#endregion
#region Get and Set base properties
private string GetCustomProperty(string propertyName)
{
var retVal = "";
var prop = base.GetPropertyValue(propertyName);
if (prop != null)
{
retVal = prop.ToString();
}
return retVal;
}
private void SetCustomProperty(string propertyName, object value)
{
var prop = base[propertyName];
if (prop != null)
{
base.SetPropertyValue(propertyName, value);
}
}
#endregion
}
然后使用以下方法应该允许您检索成员、更新属性并保存它们:
var existingMember = MemberProfile.Create(username) as MemberProfile;
existingMember.FirstName = firstName;
existingMember.Save();
令人困惑的是 Create() 方法实际上返回现有用户,他们只是出于某种原因将其称为 create ......