我有以下的代码,我想用它来修改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
方法?