gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:51 25 4
gpt4 key购买 nike

我一直在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com