我使用实体框架6与MySQL。
插入是好的,我能插入新记录。 但是当我尝试更新新创建的记录。 它更新所有行。
这是我保存的代码。
// 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();
}
当我保存,我想更新的记录,这里是更新查询。 实体框架使用User_Update存储过程,但它缺少where子句,这就是为什么更新影响的所有行。
`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
更新:增加了执行SaveAsync的SaveCommand
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);
}
}
}
现在我的问题,我怎么会告诉EF添加记录id在where子句中?
PS:这个逻辑是工作完全正常的SQL Server实体框架。 我不知道为什么它不能在MySQL中EF工作。
我还追踪是否有到库/电流的DbContext任何变化,如果有任何更改,并没有错误,我会启用保存按钮/ save命令。