Pure POCO entity update problem in repository pattern

后端 未结 3 968
迷失自我
迷失自我 2020-12-09 13:48

I have a problem in my UserRepository in which I want to update a user. I dont want certain fields updated, such as password, unless specified. For example, When I pass the

3条回答
  •  囚心锁ツ
    2020-12-09 14:33

    Can you do this?

    public User Save(User user)
        {
            if (user.UserId > 0)
            {
                User dbUser = context.Users.FirstOrDefault(u => u.UserId == user.UserId);
                //What do I do here?
                dbUser.Email = user.Email
                user = dbUser;
            }
            else
            {
                context.Users.AddObject(user);
            }
            context.SaveChanges();
            return user;
        }
    

提交回复
热议问题