3
我使用ASP.NET MVC4,我设置的自定义配置文件类在本article about universal membership providersASP.NET成员:自定义配置文件继承
public class CustomProfile : ProfileBase
{
public DateTime? Birthdate
{
get { return this["Birthdate"] as DateTime?; }
set { this["Birthdate"] = value; }
}
public static CustomProfile GetUserProfile(string username)
{
return Create(username) as CustomProfile;
}
public static CustomProfile GetUserProfile()
{
var user = Membership.GetUser();
if (user == null)
return null;
return Create(user.UserName) as CustomProfile;
}
}
说明我还更新了配置文件中的条目在web.config中定义:
<profile defaultProvider="DefaultProfileProvider" inherits="MembershipTestsV3.Models.CustomProfile">
<providers>
<add name="DefaultProfileProvider"
type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
connectionStringName="DefaultConnection"
applicationName="MyAppName" />
</providers>
</profile>
这意味着我可以实例像这样我的自定义配置文件对象时,我想:
var customProfile = HttpContext.Profile as CustomProfile;
现在,我希望有许多类型的配置文件继承此基地;如AdminUserProfile,或SupervisorProfile:
public class SupervisorProfile : CustomProfile
{
public string Department
{
get { return this["Department"] as string; }
set { this["Department"] = value; }
}
}
但是,每次我试着投我得到一个空引用异常对象:
var customProfile = HttpContext.Profile as SupervisorProfile;
我知道,在数据库级别的配置文件将只保存同一张表上的所有相关列,我只想在服务层上组织属性。有没有办法做到这一点?
全体成员计划内似乎“关闭”。一方面是通用提供者(Universal Providers)得到了很好的实现,但是属性是一个扩展的麻烦(因为它们被存储为具有通用结构的单个表上的blob),另一方面是SimpleMembership,它允许CodeFirst用于配置文件表,但与许多预先制作的管理工具(如SecurityGuard)不兼容。 – amhed