私の以前の投稿を読んでいれば、カスタムのMembershipProviderとRoleProviderを使用して、各コールで異なるデータソースをオンザフライで使用できることがわかりました。私はプロフィールで同じことをしたい。 ProfileProvider connectionstringをオンザフライで変更する
ウェブの設定ではなく、このようなカスタムクラスに格納されていない私のプロフィールのpropteries
: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);
}
}
}
:
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のために好きな私は、このようなクラスを持っています私のCustomProfileProviderとの違いは、「作成」メソッドがProfileBaseにあるため、自分自身を使用できないことです。そしてILSpyではシングルトンを見たことがあり、それが問題の原因ではないかと思います。 問題は、initializeメソッドで1回だけ渡すことです。私はデータソースを変更する別の時間を行うことができません。
あなたは私の貧しい英語を理解して助けてくれることを願っています。
この質問/回答を見る:http://stackoverflow.com/questions/10116626/modifying-profilebase-connectionstring-dynamically#comment12964962_10116626 – fuzz