1

I have the following code, which I want to use to modify the 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();

First of all the connectionStringField always comes back as null, however I can see that profile.Providers does contain MySqlProfileProvider. This is specified within my 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>

My question is how come connectionStringField is coming back as null? Does this mean I cannot modify the connection string like I normally would with a custom MembershipProvider by overriding its Initialize method?

gotnull
  • 26,454
  • 22
  • 137
  • 203
  • ASP.NET website or project template? – IrishChieftain Apr 12 '12 at 02:36
  • @IrishChieftain ASP.NET project (MVC w/ WCF w/ EF). – gotnull Apr 12 '12 at 04:00
  • Are you getting back the FirstName and Surname properties? – IrishChieftain Apr 12 '12 at 04:08
  • @IrishChieftain Yes, they're all okay and always have been. The only problem is that I'm now trying to update the connection string before the `profile.Save();` method and have been un-successful with my code. – gotnull Apr 12 '12 at 04:16
  • Fulvio, if it's EF-related I don't. Hopefully someone with more knowledge will be able to answer it. Have you been able to see the value of 'StoreConnection.ConnectionString' when debugging? – IrishChieftain Apr 12 '12 at 07:32
  • @IrishChieftain It has nothing to do with the `_connectionString` variable. It has to do with `connectionStringField ` coming back as null. – gotnull Apr 12 '12 at 23:08

1 Answers1

3

You went one too many basetypes down:

.Providers["MySqlProfileProvider"].GetType()**.BaseType**.GetField
.Providers["MySqlProfileProvider"].GetType().GetField

The following code should work:

string _connectionString = (_DataModel.Connection as System.Data.EntityClient.EntityConnection).StoreConnection.ConnectionString;
Type type = profile.Providers["MySqlProfileProvider"].GetType();
FieldInfo connectionStringField = type.GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
connectionStringField.SetValue(profile.Providers["MySqlProfileProvider"], _connectionString);
gotnull
  • 26,454
  • 22
  • 137
  • 203
Brinium
  • 46
  • 2