简体   繁体   中英

Entity Framework 5 Update works only once per object / row

I'm using Entity Framework 5 with MySQL Database and just wanted to update a row attribute "user_loginstatus" between 0 and 1. The first time when I log in via client it updates just fine for the first attempt, after trying to update again it doesn't do anything with no exception.

I log in like this:

public async void LoginExecute()
{
    // Checking Connection before etc...

    if (await _dataService.IsLoginDataValidTask(UserObj.Username, md5))
    {
        Trace.WriteLine("LoginCommand Execute: Eingeloggt");

        UserObj = await _dataService.GetUserDataTask(UserObj.Username);

        await _dataService.SetUserStatusTask(UserObj.Id, 1);
        await _dataService.WriteLog(UserObj.Id, "login", "Programm", GetLocalAdress());

        Messenger.Default.Send(UserObj);
        Messenger.Default.Send(new NotificationMessage("GoToMenuPage"));
    }
    else
    {
        // Error Stuff...
    }
}

SetUserStatus Method in DataService Class

public Task SetUserStatusTask(int id, int status)
{
    return Task.Factory.StartNew(() =>
    {
        try
        {
            var user = _entities.users.Find(id);

            user.user_loginstatus = status;
            _entities.SaveChanges();
        }
        catch (Exception ex)
        {
            Trace.WriteLine("DataService SetUserStatusTask: " + ex.Message);
        }
    });
}

GetUserData Method in DataService Class

public Task<User> GetUserDataTask(string username)
{
    return Task.Factory.StartNew(() =>
    {
        try
        {
            var user = from us in _entities.users
                       where us.user_name.Equals(username)
                       select new User
                       {
                           Id = us.user_id,
                           Username = us.user_name,
                           FirstName = us.user_firstname,
                           LastName = us.user_lastname,
                           Gender = us.user_gender,
                           Email = us.user_mail,
                           Group = us.user_usergroup,
                           Avatar = us.user_avatar,
                           LoginStatus = 1
                      };

            return user.FirstOrDefault();
        }
        catch (Exception ex)
        {
            Trace.WriteLine("DataService GetUserDataTask: " + ex);

            return null;
        }
    });
}

So "users" is my table from the database and "User" / "UserObj" my custom Object. With the Messenger (from MVVM Light) I just set via MainViewModel the Views, reset the unused ViewModels ( ViewModel = new VieModel(...); or ViewModel = null; ) and pass the current / logged in User Object.

With the same strategy I just Logout like this

public ICommand LogoutCommand
    {
        get
        {
            return new RelayCommand(async () =>
            {
                await _dataService.SetUserStatusTask(CurrentUser.Id, 0);

                if(CurrentUser.Id > 0 && IsLoggedIn)
                    await _dataService.WriteLog(CurrentUser.Id, "logout", "Programm", GetLocalAdress());

                IsLoggedIn = false;
                CurrentUser = new User();

                Messenger.Default.Send(new NotificationMessage("GoToLoginPage"));
            });
        }
    }

So I can log in with my running Client so often I want, but the "user_loginStatus" only sets the changes the first login time to 1 and back to 0, but when I log out then and login back with the same user, it wont change it anymore. When I login (still same running Client) with another user it sets again the first time the "user_loginstatus" to 1 and back to 0 and then only again when I restart my Client..

What could I do wrong?

in LoginExecute() you have UserObj, but in LogoutCommand() you have CurrentUser. Is it OK?

This is just basically from my comment regarding the original question:

I had similiar problems several times. Usually it is based on the fact that the entity you modified can't be validated properly and your dbContext fails without a proper exception because it still holds on to false entity. If this is the case you could circumvent this problem by using scoped contexts and embedding your data access operations in a using statement.

Alternatively you could try to explicitly tell EF that the entity has changes eg:

_entities.Entry(user).State = EntityState.Modified;

Regarding your other question:

In theory you shouldn't have to tell EF explicitly that the entity's values have changed. Change tracking should do that automatically. The only exception i could think of, is when you try to modify an entity that is explicitly not tracked anymore. When you call _entities.Find(id) it will look in the context if it finds the object with the matching primary key value and load it. Since you already modified this object before, the context will simply get the old object you already modified to set the login status the first time.

This "old" object is probably not tracked anymore and you have to tell EF explicitly that it has changed, by changing it's state from attached to modified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM