Rya*_*yan 5 c# profile entity-framework asp.net-mvc-3
我试图弄清楚如何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);
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中查找[我该放什么?!].这就是我遇到的问题.
在成员资格和角色服务中,它们也被初始化为null但是它们默认为它们调用:Membership.Provider或者Role.Provider,但是在这种情况下我不能使用Profile.Provider因为它不存在,所以我得到的只是一个空提供者.
我正在做一个使用个人资料会员资格的好习惯吗?
小智 2
个人资料提供程序实际上与角色和成员资格提供程序有点不同。通常,您在配置中设置配置文件键,例如..
..
<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>
Run Code Online (Sandbox Code Playgroud)
您需要做的就是实现抽象类并将其设置为在 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
http://www.davidhayden.com/blog/dave/archive/2007/10/30/CreateCustomProfileProviderASPNET2UsingLINQToSQL.aspx有一个关于 Generif 配置文件提供程序的精彩教程。
您还可以查看 .net 的 mysql 连接器源,因为它有一个自定义提供程序。
还有http://www.codeproject.com/KB/aspnet/AspNetEFProviders.aspx?display=Print
因此,简而言之,个人资料提供者是唯一一个您不像会员资格和角色提供者那样做的提供者。