Click here to Skip to main content
15,348,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have 4 tables in my database Users, Resources, Roles and a bridge table UserRoles.
I have written a PUT Request to update records in User and Resources table and along with it I have multiple entries against Users in UserRoles table which is a bridge table. So in UserRoles table I want to update RoleId against a particular user, how can I do that. I have tried so many methods but not able to update multiple entries in the bridge table. Any help is appreciated as I am new to this. I have to update entries in UserRoles table using LINQ. Here is the code below which I have written for that, please review: Thanks

What I have tried:

       public async Task<IHttpActionResult> UpdateUser(string id, [FromBody] dynamic body)
               using (hospiceEntities db = new hospiceEntities())
                   Guid guid_id = Guid.Parse(id);
                   var updateuser = db.Users.FirstOrDefault(e => e.Id == guid_id);
                   if (updateuser == null)
                       return Content(HttpStatusCode.NotFound, "User Not Found");
                       var query =
                       from resource in db.Resources
                       join user in db.Users on resource.UserId equals user.Id
                       from role in user.Roles
                       where resource.UserId == guid_id
                       select new
                       foreach (var item in query)
                           item.user.PasswordQuestion = body.passwordquestion;
                           item.user.PasswordAnswer = body.passwordanswer;
                           item.resource.FirstName = body.firstname;
                           item.resource.LastName = body.lastname;
                           item.resource.MiddleName = body.middlename;
                           item.resource.Gender = body.gender;
                           item.resource.MaritalStatus = body.maritalstatus;
                           item.resource.Ethnicity = body.ethnicity;
                           item.resource.employmentType = body.employmenttype;
                        //   item.role.Id = updateuser.Roles.Add(body.roleId);

                       await db.SaveChangesAsync();
               return Ok(Helper.SuccessResponse("User Updated"));
           catch (Exception ex)
               return BadRequest(ex.Message);
Richard Deeming 18-Jan-22 8:45am
Storing the answer(s) to the security question(s) in plain text is almost as bad as storing the password in plain text. You should treat the answer(s) like passwords, and store a salted hash instead.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Maciej Los 18-Jan-22 15:19pm
My virtual 5!
Gerry Schmitz 18-Jan-22 13:02pm
That query isn't "attached" to your DbContext; all you've done is create a local result set with a new type. Maybe you should be doing deletes and (re)inserts.
Sadia Rashid Jan2022 21-Jan-22 4:28am
can u show how?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900