在带有实体框架的新 MVC 3 应用程序中使用 ProfileProvider 是一个坏主意吗?
我一直在尝试使用实体框架将自定义 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();
}
也许我没有得到什么配置文件提供程序的要点是,但似乎我可以创建一个类来获取和设置我的配置文件表中的数据,或者当我拥有此配置文件时,是否有一些我没有看到的东西可以在控制器中使用信息设置?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对当前的 MVC3 应用程序遵循同样的思路,并最终放弃将 MembershipProvider 集成到其中。我想得越多,MembershipProvider 的使用就越少……这可能就是 API 多年来没有更新的原因:)。
I followed the same line of thinking with my current MVC3 app, and eventually gave up on integrating MembershipProvider into it. The more I think about it, the less use I see for MembershipProvider...which may be why the API hasn't been updated in years :).