MySQL Entity Framework 6 Update affecting all rows Code First

梦想与她 提交于 2019-12-25 01:49:23

问题


I'm using Entity Framework 6 with MySQL.

Inserting is fine, I was able to insert a new record. but when I tried to update the newly created record. It updates all the rows.

Here is my code for saving.

    // This is the method I use every time I save a record.
    public async Task SaveAsync(User model)
    {

        if (model.Id != 0)
        {
            var original = GetOriginal(model.Id);

            if (model.Password == string.Empty)
            {
                model.Password = original.Password;
            }
            // The state is always modified if I'm updating the record.
            var state = Context.Entry(model).State;
        }


        await base.SaveAsync();
    }

When I save the record that I want to update, here is the Update Query. Entity Framework use the User_Update stored procedure but it's missing the where clause that's why the updates affects all the rows.

`User_Update`
-- Id: '6' (Type = Int32, IsNullable = false)
-- Username: 'test123' (Type = String, IsNullable = false, Size = 7)
-- Password: 'vZxTTTgLiQUxwchySt1e1o/I9XMWm4QW5pO6MpPGI0o=' (Type = String, IsNullable = false, Size = 44)
-- Email: 'test1@gmail.com' (Type = String, IsNullable = false, Size = 15)
-- FullName: 'test1' (Type = String, IsNullable = false, Size = 5)
-- CreatedAt: 'null' (Type = DateTime, IsNullable = false)
-- UpdatedAt: 'null' (Type = DateTime, IsNullable = false)
-- RoleId: '2' (Type = Int32, IsNullable = false)
-- Executing asynchronously at 
-- Completed in 9 ms with result: 6

Updates: Added the SaveCommand that executes the SaveAsync

        private async void SaveCommandExecute(object obj)
    {
        //var passwords = (Tuple<string, string>) obj;
        //var password = passwords.Item1;
        //var confirmPassword = passwords.Item2;

        var result = await DialogHelper.ShowAsync("Are you sure you want to continue?", "Confirmation",
            MessageDialogType.YesNo);

        if (result)
        {
            await UserWrapper.ValidateAll();

            if (!UserWrapper.HasErrors)
            {

                if (UserWrapper.Id == 0)
                {
                    UserWrapper.Password = DbHelper.CalculateHash(UserWrapper.FakePassword, UserWrapper.Username);
                }

                await _userRepository.SaveAsync(UserWrapper.Model);
                HasChanges = false;
                Id = UserWrapper.Id;
                SetTitle();
                ((DelegateCommand<object>)SaveCommand).RaiseCanExecuteChanged();


                await LoadAsync(UserWrapper.Id);
            }

        }

    }

Now my question, How I will tell EF to add the record id in the where clause?

PS: This logic is working perfectly fine with SQL Server Entity Framework. I'm not sure why it's not working in MySQL EF.

I also tracked if there any changes to the repository/current dbcontext, if there are any changes and no errors, I will enable the Save Button/Save Command.

来源:https://stackoverflow.com/questions/51008874/mysql-entity-framework-6-update-affecting-all-rows-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!