views:

89

answers:

2

I'm having a hard time wrapping my head around how to use the Memberships in MVC. I know there is the built in ASPNETDB database which has all the basic tables for users and such. But what if I wanted to add a relationship between one of my custom tables and this built in user table?

If I had a database table that contained blog comments, we'll call it Comment. And each Comment had a userID associated with it could I do something like?

User.Comments.Add(someCommentObj)

Anyone know of a good article on this? Is this even possible?

Thanks

A: 

Have a look at this extensive article on the MembershipProvider:
http://www.4guysfromrolla.com/articles/120705-1.aspx

Look at Part 6 and 7, you'll probably want to implement a custom ProfileProvider and store the comment reference in the Profile.

Part 6 - capture additional user-specific information using the Profile system. Learn about the built-in SqlProfileProvider.

Part 7 - the Membership, Roles, and Profile systems are all build using the provider model, which allows for their implementations to be highly

Zyphrax
If I'm using linq to entities can these relationships be created automatically?
hanesjw
I'm not sure if there is a fully automatic way. Search for "linq to entities profileprovider". E.g.: http://www.codeproject.com/KB/aspnet/LINQCustomProfileProvider.aspx
Zyphrax
A: 

If you want to use your own custom membership tables then you'll need to build your own MembershipProvider. Matt Wrock has a walkthrough:

You'll notice that the default AccountModel allows you to inject your own provider:

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider ?? Membership.Provider;
}

Nerdinner has an example of dependency injection that you would probably find useful:

Junto