Arslan Ameer
Arslan Ameer

Reputation: 1120

USER Update with new role into database with Entity Framework

I am facing problem with updating/edit user with new role assigned or changed role from (1 or 2). Role remains same with NO Exception or Error.

NOTE: I am posting screenshots here as I want to show exactly what I am seeing.

I have a User class with role and city associated classes in it:

public class User
{   
    public City CityId { get; set; }

    public virtual Role Role { get; set; }

    public bool IsInRole(int id)
    {
        return Role != null && Role.Id == id;
    }       
}

The Role table has only 2 roles, Admin with ID=1 and guest with ID=2.

roleDBTable

I am facing problem with updating the role of an existing user.

What I am trying to do here is I want to give admin rights to a user by simply updating the user and pass 1 in role field as shown here:

View Typed-View

I am able to pass 1 or 2 id through this field into controller and manage to get "admin" by role id:

controller

As I am have the desired ROLE object in USER :

dbcontext

I'm still unable to update user with Admin role in database. It still has Guest role id.

Same scenario is with the CityID in the User class.

Any help would be appreciated

Upvotes: 2

Views: 1748

Answers (3)

Usman Haider Khan
Usman Haider Khan

Reputation: 9

You Pass the Full Object of User it will change the state of Another Table that Why First Take the User Object As it is After Changing the Data Store In to Another NEw Object and Then UPDate AND state keep unchanged of ROle Table I Hope It Work Well

 public ActionResult UpdateUser(User newUser)
    {
        newUser.City = new City { Id = Convert.ToInt32(fdata["CityList"]) };
        dbcontext db = new dbcontext();
        if (ModelState.IsValid)
        {
            using (db)
            {
                User oldUser = db.Users.Find(newUser.Id);
                oldUser.Role = newUser.Role;

                db.Entry(newUser.Role).State = EntityState.Unchanged;
                db.SaveChanges();
                return View();
            }

Upvotes: 1

Syed Bilal
Syed Bilal

Reputation: 46

The thing is you have to find the id first then make it sure the Role table should be unmodified:

public void Update(User users // this is your object`enter code here)
{
    using(_db)
    {
        User user= db.User.Find(users.id);
        user.Role_id = users.Role_id; // this is what you want to change.
        _db.Entry(Role).State = EntityState.Unchanged;
        _db.SaveChanges();
    }
}

Hope That will Help. Cheers

Upvotes: 3

Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

You need to find the ID of that specific user like:

public void UserEdit(int?id //id is your parameter here)
{
   //This is what you need.

    User user= db.User.Find(id);
    user.Role = //this is the new Role if this user
    user.CityId = //city id here
}

Update user:

public void UpdateUser(User user)
{
   db.Entry(user).State = EntityState.Modified;
   db.SaveChanges();

}

You need the user table not the role table..

//Sample class

public class User_Table
{   
    public int ID { get; set; }

    public string name { get; set; }

    public int RoleID { get; set; }
}

This table is the one you need to update not the role table as your sample from above.

Upvotes: 2

Related Questions