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:
as can see both connected each other. groups table contains following entries:
- administrator
- support
- purchasing
- 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
Post a Comment