asp.net - Entity framework (code first), many to many, users and roles -


i got mvc 3 application custom membershipprovider, stores newly registered users database. i'm using code first approach existing database (i've used entity framework power tools this). got tables users, roles , usersroles. in table users got: userid (pk) username password email ...

in table roles got roleid (pk) name description

in table usersroles got userid (set composite pk) roleid (set composite pk)

 public partial class user  {     public user()     {         this.roles = new list<role>();     }      public int userid { get; set; }     public string username { get; set; }     public string password { get; set; }     public string email { get; set; }     public virtual icollection<role> roles { get; set; } }  public partial class role {     public role()     {         this.users = new list<user>();     }     public int roleid { get; set; }     public string name { get; set; }     public string description { get; set; }     public virtual icollection<user> users { get; set; } }   public class usermap : entitytypeconfiguration<user> {     public usermap()     {         // primary key         this.haskey(t => t.userid);          // properties         this.property(t => t.username)             .isrequired()             .hasmaxlength(20);          this.property(t => t.password)             .isrequired()             .hasmaxlength(50);          this.property(t => t.email)             .isrequired()             .hasmaxlength(100);            // table & column mappings         this.totable("users");         this.property(t => t.userid).hascolumnname("userid");         this.property(t => t.username).hascolumnname("username");         this.property(t => t.password).hascolumnname("password");         this.property(t => t.email).hascolumnname("email");      } }  public class rolemap : entitytypeconfiguration<role> {     public rolemap()     {         // primary key         this.haskey(t => t.roleid);          // properties         this.property(t => t.name)             .isrequired()             .hasmaxlength(20);          this.property(t => t.description)             .hasmaxlength(50);          // table & column mappings         this.totable("roles");         this.property(t => t.roleid).hascolumnname("roleid");         this.property(t => t.name).hascolumnname("name");         this.property(t => t.description).hascolumnname("description");          // relationships         this.hasmany(t => t.users)             .withmany(t => t.roles)             .map(m =>                 {                     m.totable("usersroles");                     m.mapleftkey("roleid");                     m.maprightkey("userid");                 });       } } 

i have predefined roles in roles table , don't want them modified when new users registers (roles table should modified administrator), want new user stored in users table , assign him default role of regular user (with no special privileges), adds record users table + record in usersroles table.

   public void adduser(user user)     {            users.add(user);         savechanges();    }   var tmp = new user { username = username, password = getmd5hash(password), email = email };               using (var db = new mycontext())             {                 mycontext.adduser(userobj);             } 

but have no idea how make add new record usersroles table..with userid , roleid (default one, first 1 roleid=1 - normal user privileges). appreciated.

you fetch required role database , do

role.users.add(user); savechanges(); 

ef create new record in junction table. note users collection must loaded @ point.

doing so, don't need statement users.add(user); save user.

likewise can add role object user.roles.

when want remove record in junction table remove role loaded user.roles collection or user role.users.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -