我正在尝试弄清楚如何使用此示例中的 ProfileProvider
:http://www.codeproject.com/KB/aspnet/AspNetEFProviders.aspx
我的成员资格和角色提供者工作得很好,我已经按照示例中的方式设置了所有内容。
下面是我正在使用的类,就像成员资格和角色类一样。这将依次由我的 AccountController 调用。
public class AccountProfileService : IProfileService
{
private readonly EFProfileProvider _provider;
public AccountProfileService() : this(null) {}
public AccountProfileService(ProfileProvider provider)
{
_provider = (EFProfileProvider)(provider ?? [What do I put here?!]);
}
public void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection properties)
{
if (context == null) throw new ArgumentException("Value cannot be null or empty.", "context");
if (properties == null) throw new ArgumentException("Value cannot be null or empty.", "properties");
_provider.SetPropertyValues(context, properties);
}
}
在上面的代码中查找 [What do I put here?!]。这就是我遇到的问题。
在成员资格和角色服务中,它们也被初始化为 null 但它们默认为调用:Membership.Provider
或 Role.Provider
,但在这种情况下我不能使用 Profile.Provider
,因为它不存在,所以我得到的只是一个空提供程序。
另外,我正在做的是使用个人资料成员资格的良好做法吗?
最佳答案
配置文件提供者实际上与角色和成员资格提供者有点不同。 通常,您在配置中设置配置文件 key ,例如..
..
<profile enabled="true"
defaultProvider="CustomProfileProvider">
<providers>
<clear />
<add
name="CustomProfileProvider"
type="Providers.CustomProfileProvider, Providers"
ApplicationName="Test" />
</providers>
<properties>
<add name="ZipCode" allowAnonymous="false" />
<add name="Phone" allowAnonymous="false" />
</properties>
您需要做的就是实现抽象类并将其设置为在 web.config 中使用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Profile;
namespace blahh.Web.Source
{
class Class1 : ProfileProvider
{
public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
{
throw new NotImplementedException();
}
public override int DeleteProfiles(string[] usernames)
{
throw new NotImplementedException();
}
public override int DeleteProfiles(ProfileInfoCollection profiles)
{
throw new NotImplementedException();
}
public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
{
throw new NotImplementedException();
}
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
{
throw new NotImplementedException();
}
public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)
{
throw new NotImplementedException();
}
}
}
http://www.davidhayden.com/blog/dave/archive/2007/10/30/CreateCustomProfileProviderASPNET2UsingLINQToSQL.aspx有一个关于 generif profile provider 的很棒的教程。
您还可以查看 .net 的 mysql 连接器源代码,因为它有一个自定义提供程序。
还有http://www.codeproject.com/KB/aspnet/AspNetEFProviders.aspx?display=Print
所以简而言之,个人资料提供者是您唯一不喜欢成员资格和角色提供者的人。
关于c# - Entity framework Profile Provider example..如何初始化和设置请帮忙!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4982154/