1

I have this function: (I'm new with EF)

    public sis_user Save(sis_user user, bool edit)
    {
        if (edit)
        {
            //How to edit?
            sis_user userAux =_context.sis_user.FirstOrDefault(x => x.login == user.login);

            _context.SaveChanges();
            return user;
        }

        //To add.
        _context.sis_sis_user.Add(user);
        _context.SaveChanges();
        return user;
    }

I can add values, but I'm not sure how to edit a existing one...

2 Answers 2

1

You change the property value that you want to be changed.

public sis_user Save(sis_user user, bool edit)
    {
        if (edit)
        {
            //How to edit?
            sis_user userAux =_context.sis_user.FirstOrDefault(x => x.login == user.login);
            userAux.Name = "Different Name";
            _context.SaveChanges();
            return user;
        }

        //To add.
        _context.sis_sis_user.Add(user);
        _context.SaveChanges();
        return user;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

You could since they are of same type assuming user has id that of userAux and there are no complex types contained in these objects.
1

You just need to set the fields on the object you just pulled.

sis_user userAux =_context.sis_user.FirstOrDefault(x => x.login == user.login);

userAux.XXX = "Some Value";

_context.SaveChanges();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.