2011-02-13 44 views
5

我試圖找出如何使用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); 
    } 
} 

在上面的代碼中查找[我在這裏放什麼?!]。這是我遇到的問題。

在成員和角色服務,他們也被初始化爲空,但他們默認設置,以便他們稱之爲之一:Membership.ProviderRole.Provider,但在這種情況下,因爲它不存在,我無法使用Profile.Provider,所以我得到的是null提供者。

此外,我正在做一個良好的做法,使用配置文件會員資格?

+0

你使用任何Ioc? – 2011-02-27 14:35:17

回答

2

配置文件提供程序實際上與角色和成員資格提供程序有點不同。 通常設置在config配置文件鍵如..

..

<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> 

所有你需要做的是落實抽象類,並將其設置在網絡中使用。配置。


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

所以簡而言之配置文件提供者是你不喜歡做的成員資格和角色提供了唯一的一個。