我有下面的代碼,我想用它來修改ProfileBase
的connectionString:修改ProfileBase ConnectionString的動態
ProfileBase profile = ProfileBase.Create(username);
string _connectionString = (_DataModel.Connection as System.Data.EntityClient.EntityConnection).StoreConnection.ConnectionString;
FieldInfo connectionStringField = profile.Providers["MySqlProfileProvider"].GetType().BaseType.GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
connectionStringField.SetValue(profile.Providers["MySqlProfileProvider"], _connectionString);
profile["FirstName"] = firstName;
profile["Surname"] = surname;
profile.Save();
首先所有connectionStringField
總是回來爲空,但我可以看到profile.Providers
確實包含MySqlProfileProvider
。這是內指定我Web.Config
:
<profile defaultProvider="MySqlProfileProvider">
<providers>
<clear/>
<add name="MySqlProfileProvider" connectionStringName="MyApp" applicationName="MyApp" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
<properties>
<add allowAnonymous="false" defaultValue="" name="FirstName" readOnly="false" serializeAs="String" type="System.String"/>
<add allowAnonymous="false" defaultValue="" name="Surname" readOnly="false" serializeAs="String" type="System.String"/>
</properties>
</profile>
我的問題是怎麼來的connectionStringField
是回來爲空?這是否意味着我無法像修改自定義的MembershipProvider
那樣通過重寫Initialize
方法修改連接字符串?
ASP.NET網站或者項目模板? – IrishChieftain 2012-04-12 02:36:22
@IrishChieftain ASP.NET項目(MVC w/WCF w/EF)。 – fuzz 2012-04-12 04:00:27
你是否獲得名字和姓氏屬性? – IrishChieftain 2012-04-12 04:08:14