Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework Profile Provider example.. How to initialize and setup please help!

I am trying to figure out how to use the ProfileProvider that's in this example: http://www.codeproject.com/KB/aspnet/AspNetEFProviders.aspx

I've got the membership and role providers working great, I've got everything setup exactly how it is in the example.

Below is the class that I'm using just like the membership and roles classes. This would in turn be called by my 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);
    }
}

In the code above look for [What do I put here?!]. This is what I'm having a problem with.

In the membership and role services they also are initialized as null but they default so they call either: Membership.Provider or Role.Provider, but in this case I can't use Profile.Provider as it doesn't exist, so all I get is a null provider.

Also are is what I'm doing a good practice for using a profile membership?

like image 694
Ryan Avatar asked Nov 06 '22 02:11

Ryan


1 Answers

The profile provider is actually a bit different than the role and membership providers. Typically you set profile keys in the config such as..

..

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

All you need to do is implement the abstract class and set it to be used in the 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 has a great tutorial on generif profile provider.

Also you can take a look at the mysql connector source for .net as it has a custom provider.

There is also http://www.codeproject.com/KB/aspnet/AspNetEFProviders.aspx?display=Print

So in a nutshell the profile provider is the only one you dont do like the membership and role providers.

like image 119
Dimentox Avatar answered Nov 09 '22 06:11

Dimentox