保存数据库上下文时数据被更改

时间:2018-09-18 21:06:29

标签: asp.net entity-framework-6

在下面的代码中,我有一个newuserStatus对象的UserId和StatusByUserId属性。我将UserId设置为来自用户对象的任何ID id,并将StatusByUserId设置为1。运行SaveChanges时,StatusByUserId被更改为UserId,我不想发生这种情况,我不确定这是不可能的。任何建议将不胜感激,谢谢。

[HttpPost]
    public async Task<IActionResult> AddUser([FromBody]UserPageViewModel userPageModels)
    {
        Users user = userPageModels.Users;

        foreach (Clients client in userPageModels.ListOfClients)
        {
            ClientUsers clientUser = new ClientUsers();
            //clientUser.User = user;
            //clientUser.Client = client;
            clientUser.ClientId = client.ClientId;
            clientUser.UserId = user.UserId;
            user.ClientUsers.Add(clientUser);
        }
        UserStatusHistory newUserStatus = new UserStatusHistory();
        newUserStatus.StatusDate = DateTime.Now;
        newUserStatus.UserStatusId = user.UserStatusId;
        //newUserStatus.User = user;
        newUserStatus.StatusChangedByUser = user;

        newUserStatus.UserId = user.UserId;
        newUserStatus.StatusChangedByUserId = 1;
        //user.UserStatusHistoryUser.Add(newUserStatus);
        //Todo change this to current User;            
        _gatewaycontext.Users.Add(userPageModels.Users);
        await _gatewaycontext.SaveChangesAsync();
        return Ok(CreatedAtRoute("GetUser", new Users { UserId = user.UserId }, userPageModels));
    }

0 个答案:

没有答案