c# - How to set group of a new user? -


this might seem easy question, can't figure out answer.

i have user table , groups table in database looks this:

enter image description here

as can see both connected each other. groups table contains following entries:

  1. administrator
  2. support
  3. purchasing
  4. selling

now want add new user following code using entity framework:

private void adduser() {     using (var context = new azubitestentities())     {         var user = new benutzer();          user.name = username;         user.passwort = generatehashpassword();          user.gruppen = ???          user.anlegedatum = datetime.now;         user.loeschdatum = null;         user.erstellerid = 1;          context.benutzer.add(user);          context.savechanges();     } } 

how can set group of user?

since miss direct one-on-many foreign key gruppenid in benutzer table, assume you're using many-to-many middle table.

in case (where don't see middle foreign key) have group database , assign user.gruppen property.

private void adduser() {     using (var context = new azubitestentities())     {         var user = new benutzer();         var gruppen = context.gruppen.first(x => x.gruppenid == [group id]);          user.name = username;         user.passwort = generatehashpassword();          user.gruppen = gruppen;          user.anlegedatum = datetime.now;         user.loeschdatum = null;         user.erstellerid = 1;          context.benutzer.add(user);          context.savechanges();     } } 

more details here: https://msdn.microsoft.com/en-us/data/jj713564.aspx


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -