0

I have an ASP.NET WebForms application with a (EF) database.

Basically the two tables I'm concerned with are Users and Roles. In Roles there's: Id (pk), UserId (fk), Type : String - which contains either Admin, User, Moderator, Publisher, etc. In Users there's: Id (pk), Email, NameFirst, NameLast, Password, Username.

In designer I connected Users with Roles so that in Roles -> UserId == Id of User.

And now, after creating a class that inherits from RoleProvider, in function GetRolesForUser(string username) I want to get the enum of all the roles of a user whose id is that of the username.

So for instance if I get a user Agon, I want to be able to get an enum of all his roles for later use and also return them as string[] in said method.

So for after hours of head-numbing attempts I've been getting consistent errors. Not sure where to go from here:

    public override string[] GetRolesForUser(string username)
    {
        using (SMEntities db = new SMEntities())
        {
            User user = db.Users.First(x => x.Username == username);
        }

        //throw new NotImplementedException();
    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Agon Noga
  • 563
  • 1
  • 9
  • 17
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Feb 11 '13 at 22:19
  • My apologies, I've been absent for a long time. – Agon Noga Feb 11 '13 at 22:31
  • 1
    No apology necessary - now you know. – John Saunders Feb 11 '13 at 22:33

1 Answers1

1

I'm not sure where enums come into play really on this one, but how about the following:

    using (SMEntities db = new SMEntities())
    {
        User user = db.Users.First(x => x.Username == username);
        return user.Roles.Select(r => r.Type).ToArray();
    }
Corey Adler
  • 15,897
  • 18
  • 66
  • 80
  • First, thank you for the input! I tried all sorts of Where and Take and Select but none ended up not generating errors, and this does! As for the enums, I'd like to use them later when it comes to actually deciding the permission and uses of each role and whatnot. Which brings me to the point that I'd like to avoid boxing and unboxing and just stick to a single enum that can be used by the Roles class as well. – Agon Noga Feb 11 '13 at 22:10
  • It isn't. The line of code you provided works for me. Might have worded it wrongly. My bad. – Agon Noga Feb 11 '13 at 22:20
  • You should mark this as the answer, then. As far as enums go, Entity Framework 5 now has a way to handle enums so that you can easily make Type into an enum, have your method return a list (or array) of those enums, and select them using LINQ. – Corey Adler Feb 11 '13 at 22:23
  • I'm not sure I understand what you mean by "have your method return a list (or array) of those enums, and select them using LINQ," could you elaborate? As in, which method exactly? GetRolesForUser? I'm not handing the return, after all. And what does 'select using LINQ' mean? – Agon Noga Feb 11 '13 at 22:25
  • 1
    i.e. Your GetRolesForUser call would have a return type of the RoleType enum that you will create. Select using LINQ would be exactly like what I did above, which is `user.Roles.Select(r => r.Type)`. Only it will return the enums themselves (once you've converted it to being an enum, of course). – Corey Adler Feb 11 '13 at 22:28
  • I see. I'll get to thinking about this now, then. – Agon Noga Feb 11 '13 at 22:31