ProfileProvider 重写问题。。。
我自己定义了一个
MyProfile 类继承自ProfileProvider
Config 是这样定义的
<anonymousIdentification enabled="true"/>
<profile defaultProvider="ProfileConnection">
<providers>
<clear/>
<add name="ProfileConnection" connectionStringName="ProfileConnection" type="Profile.WaWaProfileProvider" applicationName="WaWa Beta 1.0"/>
</providers>
<properties>
<clear/>
<add name="AccountInfo" type="Models.Account" provider="ProfileConnection" allowAnonymous="true" />
</properties>
为什么 IDE 里面就是不提示ProfileCommon 下的Profile 属性
ProfileConnection 是<connectionStrings>下的 某连接
Models.Account是Model 下的Account 实体
连接没有问题
不知道是什么原因
贴出自己的Profile
public sealed class WaWaProfileProvider : ProfileProvider
{
// Get an instance of the ProfileDAL using the ProfileDALFactory,though you can Config the Web.Config to Change Reflection
private static readonly IProfileDAL.IProfileProvider dal = ProfileFactory.DataAccess.CreatePetShopProfileProvider();
// Private members
private const string ERR_INVALID_PARAMETER = "Invalid Profile parameter:";
private const string PROFILE_ACCOUNT = "Account";//Account Information
private static string applicationName = "WaWa Beta 1.0";
#region "ProfileProvider"
/// <summary>
/// Retrieve Application Name
/// </summary>
public override string ApplicationName
{
get
{
return applicationName;
}
set
{
applicationName = value;
}
}
/// <summary>
/// Initial Function From ProfileBase
/// </summary>
/// <param name="name">Set a Friendly Name</param>
/// <param name="config">Name/Value Collection</param>
public override void Initialize(string name, NameValueCollection config)
{
if (config == null)
throw new ArgumentNullException("config");
if (string.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "WaWa Custom Profile Provider");
}
if (string.IsNullOrEmpty(name))
name = "WaWaProfileProvider";
if (config["applicationName"] != null && !string.IsNullOrEmpty(config["applicationName"].Trim()))
applicationName = config["applicationName"];
base.Initialize(name, config);
}
/// <summary>
/// Retrieve SettingsPropertyValueCollection of Profile Property Value
/// </summary>
/// <param name="context">SettingsContext(Hashtable instance,Get a key/value Collection for Profile R/W)</param>
/// <param name="collection"></param>
/// <returns></returns>
public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
{
string username = (string)context["UserName"];
bool isAuthenticated = (bool)context["IsAuthenticated"];
SettingsPropertyValueCollection svc = new SettingsPropertyValueCollection();
foreach(SettingsProperty prop in collection) {
SettingsPropertyValue pv = new SettingsPropertyValue(prop);
switch(pv.Property.Name) {
case PROFILE_ACCOUNT:
if(isAuthenticated)
pv.PropertyValue = GetAccountInfo(username);
break;
default:
throw new ApplicationException(ERR_INVALID_PARAMETER + " name.");
}
svc.Add(pv);
}
return svc;
}
private object GetAccountInfo(string username)
{
return dal.GetAccountInfo(username, applicationName);
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{
string username = (string)context["UserName"];
CheckUserName(username);
bool isAuthenticated = (bool)context["IsAuthenticated"];
int uniqueID = dal.GetUniqueID(username, isAuthenticated, false, ApplicationName);
if (uniqueID == 0)
uniqueID = dal.CreateProfileForUser(username, isAuthenticated, ApplicationName);
foreach (SettingsPropertyValue pv in collection)
{
if (pv.PropertyValue != null)
{
switch (pv.Property.Name)
{
case PROFILE_ACCOUNT:
if (isAuthenticated)
SetAccountInfo(uniqueID, (Account)pv.PropertyValue);
break;
default:
throw new ApplicationException(ERR_INVALID_PARAMETER + " name.");
}
}
}
}
private static void SetAccountInfo(int uniqueID, Account account)
{
dal.SetAccountInfo(uniqueID, account);
}
private static bool DeleteProfile(string username)
{
CheckUserName(username);
return dal.DeleteProfile(username, applicationName);
}
private static void CheckUserName(string userName)
{
if (string.IsNullOrEmpty(userName) || userName.Length > 256 || userName.IndexOf(",") > 0)
throw new ApplicationException(ERR_INVALID_PARAMETER + " user name.");
}
/// <summary>
/// Delete Profiles
/// </summary>
/// <param name="profiles">A System.Web.Profile.ProfileInfoCollection of information about profiles that are to be deleted.</param>
/// <returns>The number of profiles deleted from the data source.</returns>
public override int DeleteProfiles(ProfileInfoCollection profiles)
{
int deleteCount = 0;
foreach (ProfileInfo p in profiles)
if (DeleteProfile(p.UserName))
deleteCount++;
return deleteCount;
}
/// <summary>
///
/// </summary>
/// <param name="usernames"></param>
/// <returns></returns>
public override int DeleteProfiles(string[] usernames)
{
int deleteCount = 0;
foreach (string user in usernames)
if (DeleteProfile(user))
deleteCount++;
return deleteCount;
}