<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>

[{'answer_id': 80594, 'author': 'Mladen Mihajlovic', 'author_id': 11421, 'author_profile': 'https://Stackoverflow.com/users/11421', 'pm_score': 3, 'selected': False, 'text': "<p>Not sure about the whole question, but one thing I noticed in your code:</p>\n\n<pre><code>ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;\n</code></pre>\n\n<p>You do not need both the (ProfileCommon) and the as ProfileCommon. They both do casts, but the () throws and exception while the as returns a null if the cast can't be made.</p>\n"}, {'answer_id': 93963, 'author': 'Dave Dunkin', 'author_id': 453303, 'author_profile': 'https://Stackoverflow.com/users/453303', 'pm_score': 3, 'selected': False, 'text': '<p>Try <a href="http://code.msdn.microsoft.com/WebProfileBuilder" rel="nofollow noreferrer">Web Profile Builder</a>. It\'s a build script that automagically generates a WebProfile class (equivalent to ProfileCommon) from web.config.</p>\n'}, {'answer_id': 260926, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The web.config file in the MVC Beta is wrong. The SqlProfileProvider is in System.Web.Profile, not System.Web.Security. Change this, and it should start working for you.</p>\n'}, {'answer_id': 434793, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 7, 'selected': True, 'text': '<p>Here\'s what you need to do:</p>\n\n<p>1) In Web.config\'s  section, add "inherits" attribute in addition to your other attribute settings:</p>\n\n<pre><code>&lt;profile inherits="MySite.Models.ProfileCommon" defaultProvider="....\n</code></pre>\n\n<p>2) Remove entire <code>&lt;properties&gt;</code> section from Web.config, since you have already defined them in your custom ProfileCommon class and also instructed to inherit from your custom class in previous step</p>\n\n<p>3) Change the code of your ProfileCommon.GetProfile() method to </p>\n\n<pre><code>public virtual ProfileCommon GetProfile(string username)        \n{            \n     return Create(username) as ProfileCommon;      \n}\n</code></pre>\n\n<p>Hope this helps.</p>\n'}]
['https://Stackoverflow.com/questions/79129', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10792/']