2

I've been trying to add a custom ProfileProvider class to my MVC 3 app with entity framework.I noticed its calling GetPropertyValues everytime I try and do something like:

profile.FirstName = viewModel.FirstName;

For some reason I can't see to update the values in the database here is what I have setup so far.I've been following roughly this tutorial: http://www.davidhayden.com/blog/dave/archive/2007/10/30/CreateCustomProfileProviderASPNET2UsingLINQToSQL.aspx

<profile enabled="true" defaultProvider="EfProfileProvider" inherits="Data.BOHProfile" automaticSaveEnabled="true">
    <providers>
        <clear />
        <add name="EfProfileProvider" type="Data.Providers.EfProfileProvider" connectionStringName="BOHEntities" applicationName="BOH" />
    </providers>
</profile>

Profile class:

public class BOHProfile : ProfileBase, IBOHProfile
{
    public virtual string FirstName
    {
        get { return base["FirstName"] as string; }
        set { base["FirstName"] = value; }
    }

    public virtual string LastName
    {
        get { return base["LastName"] as string; }
        set { base["LastName"] = value; }
    }

    public virtual string City
    {
        get { return base["City"] as string; }
        set { base["City"] = value; }
    }

    public static BOHProfile CurrentUser()
    {
        return (BOHProfile)(ProfileBase.Create(Membership.GetUser().UserName));
    }

    public static BOHProfile CurrentUser(string userName)
    {
        return (BOHProfile)(ProfileBase.Create(userName));
    }
}

Heres the code in the actual provider:

    public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
    {   
        var uow = new EfUnitOfWork();
        var profileRepo = new ProfileRepository(new EfRepository<Profile>(), uow);
        var data = profileRepo.FetchProfileData(context["UserName"].ToString(), ApplicationName);

        return data != null ? ProviderUtility.ConvertToSettingsPropertyValueCollection(data) : null;
    }

    public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
    {
        var data = ProviderUtility.ConvertFromSettingsPropertyValueCollection(collection);
        var uow = new EfUnitOfWork();
        var profileRepo = new ProfileRepository(new EfRepository<Profile>(), uow);

        profileRepo.SaveProfileData(context["UserName"].ToString(), ApplicationName, data);
        profileRepo.Save();
    }

Maybe I'm not getting what the point of the profile provider is but it seems like I could just make a class that gets and sets the data in my profile table, or is there something I'm not seeing that I can perhaps use in the controllers when I have this profile information setup?

4

1 回答 1

0

我对当前的 MVC3 应用程序遵循了相同的思路,最终放弃了将 MembershipProvider 集成到其中。我想得越多,我看到 MembershipProvider 的使用就越少......这可能是 API 多年来没有更新的原因:)。

于 2011-04-14T03:49:55.713 回答