c# - 在带有 Entity Framework 的新 MVC 3 应用程序中使用 ProfileProvider 是个坏主意吗?

标签 c# asp.net-mvc profile asp.net-profiles

我一直在尝试使用 Entity Framework 将自定义 ProfileProvider 类添加到我的 MVC 3 应用程序中。我注意到每次我尝试执行以下操作时它都会调用 GetPropertyValues:

profile.FirstName = viewModel.FirstName;

出于某种原因,我看不到更新数据库中的值,这是我到目前为止所设置的。我一直在大致遵循本教程: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>

个人资料类:

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));
    }
}

这是实际提供程序中的代码:

    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();
    }

也许我不明白配置文件提供者的意义是什么,但似乎我可以创建一个类来获取和设置我的配置文件表中的数据,或者有什么我看不到的东西我可以当我有这个配置文件信息设置时,也许在 Controller 中使用?

最佳答案

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

关于c# - 在带有 Entity Framework 的新 MVC 3 应用程序中使用 ProfileProvider 是个坏主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5644323/

相关文章:

c# - ASP.NET 核心 : How to pass connection string to DBContext?

c# - 通过数组传递给构造函数

c# - 从技术上讲, "roaming"是什么意思?

c# - 如何在 SQL Server 数据库中存储 C# 属性并调用命令?

c# - MiniProfiler Start 和 StartNew 不按照文档工作

c# - 为异步方法添加同步缓存机制 "transparently"

c# - 如何将 JavaScript 数组传递给 ASP.Net MVC Action 作为帖子的一部分

c++ - 如何评估程序的运行时?

java - JAR 文件逐渐增大用户文件夹的大小

Android 到 Drupal 用户创建