title: Implementing Profile Provider in ASP.NET MVC
body: <p>For the life of me, I cannot get the SqlProfileProvider to work in an MVC project that I'm working on.</p>
  
  <p>The first interesting thing that I realized is that Visual Studio does not automatically generate the ProfileCommon proxy class for you.  That's not a big deal since it's simpy a matter of extending the ProfileBase class.  After creating a ProfileCommon class, I wrote the following Action method for creating the user profile.</p>
  
  <pre><code>[AcceptVerbs("POST")]
  public ActionResult CreateProfile(string company, string phone, string fax, string city, string state, string zip)
  {
      MembershipUser user = Membership.GetUser();
      ProfileCommon profile = ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;
  
      profile.Company = company;
      profile.Phone = phone;
      profile.Fax = fax;
      profile.City = city;
      profile.State = state;
      profile.Zip = zip;
      profile.Save();
  
      return RedirectToAction("Index", "Account"); 
  }</code></pre>
  
  <p>The problem that I'm having is that the call to ProfileCommon.Create() cannot cast to type ProfileCommon, so I'm not able to get back my profile object, which obviously causes the next line to fail since profile is null.</p>
  
  <p>Following is a snippet of my web.config:</p>
  
  <p><pre><code>&lt;profile defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true"&gt;
      &lt;providers&gt;
          &lt;clear/&gt;
          &lt;add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" /&gt;
      &lt;/providers&gt;
      &lt;properties&gt;
          &lt;add name="FirstName" type="string" /&gt;
          &lt;add name="LastName" type="string" /&gt;
          &lt;add name="Company" type="string" /&gt;
          &lt;add name="Phone" type="string" /&gt;
          &lt;add name="Fax" type="string" /&gt;
          &lt;add name="City" type="string" /&gt;
          &lt;add name="State" type="string" /&gt;
          &lt;add name="Zip" type="string" /&gt;
          &lt;add name="Email" type="string" &gt;
      &lt;/properties&gt;
  &lt;/profile&gt;</pre></code></p>
  
  <p>The MembershipProvider is working without a hitch, so I know that the connection string is good.</p>
  
  <p>Just in case it's helpful, here is my ProfileCommon class:</p>
  
  <pre><code>public class ProfileCommon : ProfileBase
      {
          public virtual string Company
          {
              get
              {
                  return ((string)(this.GetPropertyValue("Company")));
              }
              set
              {
                  this.SetPropertyValue("Company", value);
              }
          }
  
          public virtual string Phone
          {
              get
              {
                  return ((string)(this.GetPropertyValue("Phone")));
              }
              set
              {
                  this.SetPropertyValue("Phone", value);
              }
          }
  
          public virtual string Fax
          {
              get
              {
                  return ((string)(this.GetPropertyValue("Fax")));
              }
              set
              {
                  this.SetPropertyValue("Fax", value);
              }
          }
  
          public virtual string City
          {
              get
              {
                  return ((string)(this.GetPropertyValue("City")));
              }
              set
              {
                  this.SetPropertyValue("City", value);
              }
          }
  
          public virtual string State
          {
              get
              {
                  return ((string)(this.GetPropertyValue("State")));
              }
              set
              {
                  this.SetPropertyValue("State", value);
              }
          }
  
          public virtual string Zip
          {
              get
              {
                  return ((string)(this.GetPropertyValue("Zip")));
              }
              set
              {
                  this.SetPropertyValue("Zip", value);
              }
          }
  
          public virtual ProfileCommon GetProfile(string username)
          {
              return ((ProfileCommon)(ProfileBase.Create(username)));
          }
      }</code></pre>
  
  <p>Any thoughts on what I might be doing wrong?  Have any of the rest of you successfully integrated a ProfileProvider with your ASP.NET MVC projects?</p>
  
  <p>Thank you in advance...</p>