views:

425

answers:

1

Hi,

I have a model, Docs, with a guid column, author_id, which should reference a MembershipUser.

I'm using the standard MembershipProvider (I only changed the db name), and of course I haven't a model for MembershipUsers.

I can easily define some methods in class Docs to get MembershipUser for a Doc and all Docs for a MembershipUser, but some things are tricky (listing users, show how many docs each user has); furthermore, I feel I'm using MVC in an odd way: it seems to me that it would be easier if I had a MembershipUsers model...

How can I implement this relationship? Should I implement a model for MembershipUsers?

thanks

update: I realize I wasn't clear at all.

Say I want to list all my users and their docs. One way to do this is:

ViewModel model = new ViewModel()
{
    Users = from MembershipUser user in Membership.GetAllUsers(page, pagesize, out totalusers)
            select new UserWithDocs
            {
                User = user,
                Docs = context.Docs.Where(c => c.author_id == (Guid) user.ProviderUserKey)
            },
    UsersCount = totalusers
};

This works, but generates one separate query for each user.

I could get an array of users' guids and then query for Docs where author_id IN list_of_guids, but then I should manually associate each doc to its author.

What is a better solution?

A: 

not sure if i understand you correctly, but you can use the MembershipUser class as your model for users. if you want to reference user to your "doc" model, you can simply add a property "Author" to you model class and when you fetch the model from DB you can get the user as well.

the Membership class contains some static methods to list all users, add/delete users etc.

yosig81