如果您阅读了我以前的文章,您可以使用自定义MembershipProvider和RoleProvider在每次通话中使用不同的数据源。我想和Profile一样。动态更改ProfileProvider连接字符串
我的个人资料propteries在网络配置,但在自定义类这样不存储:
public class AccountProfile : ProfileBase
{
public override SettingsProviderCollection Providers
{
get
{
return base.Providers;
}
}
static public AccountProfile GetUserProfile(string userName)
{
return (AccountProfile)(ProfileBase.Create(userName));
}
[SettingsAllowAnonymous(false)]
public string MobilePhone
{
get { return ((string)(base["MobilePhone"])); }
set { base["MobilePhone"] = value; Save(); }
}
}
也想为会员和RoleProvider我有这样一个类:
public class MyProfileProvider : SqlProfileProvider
{
public MyProfileProvider()
{
}
public MyProfileProvider(string SubDomainInstanceName)
{
string configPath = "~/web.config";
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
ProfileSection section = (ProfileSection)config.GetSection("system.web/profile");
ProviderSettingsCollection settings = section.Providers;
NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters;
Initialize(section.DefaultProvider, membershipParams);
}
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
if (!string.IsNullOrEmpty(instance))
{
// Update the private connection string field in the base class.
string connectionString = "";//my connection
// Set private property of Membership provider.
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
connectionStringField.SetValue(this, connectionString);
}
}
}
我的CustomProfileProvider之间的差异是我不能使用自己,因为“创建”方法在ProfileBase中。和ILSpy一样,我看到了一个单例,我不知道它是不是问题的根源。 问题是我只在初始化方法中传递一次。我不能再花时间改变数据源。
我希望你能理解我可怜的英语,能帮助我。
看到这个问题/答案︰http://stackoverflow.com/questions/10116626/modifying-profilebase-connectionstring-dynamically#comment12964962_10116626 – fuzz 2012-04-13 01:18:39